相关疑难解决方法(0)

为什么我不能在没有main方法的情况下实例化和创建Object?(堆栈溢出错误)

我的代码:

(导致堆栈溢出错误)

public class Overloads {
    String uniqueID;    
    Overloads ov2=new Overloads();

    public static void main(String[] args) {
        System.out.println("IN MAIN");
    }

    public void setUniqueID(String theID) {
        // II lots of validation code, and then:
        uniqueID = theID;
        System.out.println(uniqueID);
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码工作正常:

public class Overloads {
    String uniqueID;   

    public static void main(String[] args) {
          Overloads ov2=new Overloads();
          System.out.println("IN MAIN");
    }

    public void setUniqueID(String theID) {
        // II lots of validation code, and then:
        uniqueID = theID;
        System.out.println(uniqueID);           
    }
}
Run Code Online (Sandbox Code Playgroud)

java

1
推荐指数
1
解决办法
2059
查看次数

标签 统计

java ×1