如何将非静态(动态实例)对象用作 return 用于 Java 中的静态方法?

How can I use a non-static (dynamic instance) object as return for a static method in Java?

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

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

示例:

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;
}

上面的效果很好,但是当我使用自己的 class 到 return 值时,编译器会给我 "non-static variable this cannot be referenced from a static context" (注意:编辑以表明这些classes 嵌套在 JInputs class 中,这显然是重现错误所必需的):

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 class 成为静态的 class 但随后对该方法的调用不会更改原始值称呼? Java 程序员如何 return 从静态方法整理数据?为什么它允许实例化内置 classes 但不允许实例化用户定义的 classes?

此代码的唯一问题是花括号放错了位置:

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?

确实没有您所描述的 "static class" 的概念。静态 class 只是一个内部 class,无需外部 class 的实例即可访问。它的所有成员仍然像普通成员一样 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不区分"built-in"classes和"user-defined"classes.

看你的错误,你的问题可能遗漏了一些代码,大概是这样的:

public class SomeClass {
      public class UserData {
            ....
      }

      public static UserData decodeText(String codeString) {
            UserData data = new UserData();
            ....
      }
}

内类

所以你在使用内在的概念类。这些 classes 需要有权访问其父 class 的实例才能创建(此处 UserData 需要有权访问 SomeClass 的实例)。当通过 "this" 指针从非静态方法中创建内部 class 时,JVM 提供此访问。但是,在静态方法中无法访问 "this":这就是编译器告诉您的内容:无法创建 UserData,因为它需要访问此 ("non-static variable this cannot be referenced from a static context").

但是您可以在 SomeClass 或 UserData 及其任何子classes 的任何非静态方法中创建 UserData 的新实例。

您的用例

在您的情况下,您似乎不需要内部 class。当您想要访问父 class 的成员时,您只需要一个嵌套的 class。否则静态嵌套 class 就足够了。

你的问题

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并不意味着它的变量是静态的,或者它是一个singleton。静态 class 基本上类似于标准 class,只是它嵌套在另一个 classes 定义中。 有关静态和非静态嵌套 classes 之间区别的更多信息,请参阅 Oracle documentation