java - 如何使用非静态(动态实例)对象作为 Java 中静态方法的返回值?

标签 java static return

<分区>

我搜索了 stackoverflow 以及其他一些网站,遗憾的是没有找到这个问题,更不用说回答了。也许我的方法最好以另一种方式尝试?我是 Java 新手;我认为这应该是一个非常简单的答案。

问题: 我有一个static 方法,我想从中返回值。为了方便和整洁,我想使用我自己的类而不是 ArrayListString[] 或类似的类。问题是我无法在静态方法中实例化我的类(正如我预期的那样可能是一个问题)。有趣的是:使用 String[]Object 作为返回值确实有效(这是这些类的一个实例)...所以为什么我不能使用自己的类实例?

示例:

public static String[] decodeText(String codeString) {
    //Parse codestring and return values (not included in this example)
    String[] data = new String[3];
    data[0]="This";
    data[1]="does";
    data[2]="work";                
    return data;
}

上面的效果很好,但是当我使用自己的类返回值时,编译器会给我“无法从静态上下文中引用非静态变量 this(注意:已编辑以表明这些类嵌套在 JInputs 类中,这显然是重现错误所必需的):

public class JInputs extends JOptionPane {    
    //A lot of missing code here (which shouldn't be necessary to reproduce issue)

    public class UserData {
        public String userName;
        public String code;
        public long milliTime;

        UserData() {            
        }
        UserData(String userName, String code, long milliTime) {
            this.userName = userName;
            this.milliTime = milliTime;
            this.code = code;
        }
    }

    public static UserData decodeText(String codeString) {
        //Parse codestring and return values (not included in this example)
        UserData data = new UserData();
        data.milliTime = System.currentTimeMillis();
        data.code = "blah";
        data.userName = "Me";                
        return data;
    }
}

显然,我可以将我的 UserData 类设为静态类,但随后对该方法的调用不会更改原始调用的值吗? Java 程序员如何从静态方法返回整洁的数据?为什么它允许实例化内置类而不是用户定义类?

最佳答案

这段代码唯一的问题是花括号放错了地方:

public class UserData {
    public String userName;
    public String code;
    public long milliTime;

    UserData() {            
    }
    UserData(String userName, String code, long milliTime) {
        this.userName = userName;
        this.milliTime = milliTime;
        this.code = code;
    }
} //end of class!

//this method is outside the class!
public static UserData decodeText(String codeString) {
    //Parse codestring and return values (not included in this example)
    UserData data = new UserData();
    data.milliTime = System.currentTimeMillis();
    data.code = "blah";
    data.userName = "Me";                
    return data;
}

我想你想要的是这个:

public class UserData {
    public String userName;
    public String code;
    public long milliTime;

    UserData() {            
    }
    UserData(String userName, String code, long milliTime) {
        this.userName = userName;
        this.milliTime = milliTime;
        this.code = code;
    }


    public static UserData decodeText(String codeString) {
        //Parse codestring and return values (not included in this example)
        UserData data = new UserData();
        data.milliTime = System.currentTimeMillis();
        data.code = "blah";
        data.userName = "Me";                
        return data;
    }
}

The above works great but when I use my own class to return values the compiler gives me the "non-static variable this cannot be referenced from a static context"

您发布的代码不会导致该错误。您要么复制了错误的代码,要么正在查看旧错误。

Obviously, I could make my UserData class a static class but then wouldn't subsequent calls to the method change the values of the original call?

确实没有您所描述的“静态类”的概念。静态类只是一个内部类,无需外部类的实例即可访问。它的所有成员仍然表现得像普通类(class)的成员。

How do Java programmers return neat data from static methods? Why does it allow built-in classes to be instantiated but not user defined classes?

您发布的内容可以正常工作。 Java 不区分“内置”类和“用户定义”类。

关于java - 如何使用非静态(动态实例)对象作为 Java 中静态方法的返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30602252/

相关文章:

java - 接口(interface)的默认实现?或保留为空

c++ - 函数局部静态变量 : pros/cons from performance point of view

java - 返回数组列表的常量引用

javascript - 有什么方法可以判断 javascript 函数是否已返回?

c - 如何从 C 函数返回二维数组?

Java Spring注入(inject)

java - 使用 Java 为 POS 创建自定义收据

java - 解析日期的优雅解决方案

java - 如何使用 AspectJ 拦截 java 类中的静态属性?

c# - 是否可以将 Ninject 与静态属性一起使用?