public class Test { private Object obj = new Object(); }
谁告诉你不能在main()方法体外或者在静态方法外new对象啊???
楼主的意思是new自身吧,这个还真不知道
都可以,你就是在自己类里面new个自己的也是可以 public class SingletonTest { public SingletonTest singleton = new SingletonTest(); public SingletonTest newObject(){ return new SingletonTest(); } }
但这个不行啊,这个是一样的啊 class Test11 { public Test11 t1 = new Test11(); public Test11 newObject(){ return new Test11(); }public static void main(String[] args) { } }
private Object obj = new Object();
}
public class SingletonTest { public SingletonTest singleton = new SingletonTest();
public SingletonTest newObject(){
return new SingletonTest();
}
}
static TestSuper aa=new TestSuper(); //就是不知为什么去掉static编译没问题
//,运行就会出错。
static void f()
{
TestSuper aaaaa=new TestSuper();
}
public TestSuper()
{
super("hello");
System.out.println("这里是子类的无参构造方法");
}
public TestSuper(String s)
{
super(s,"parameter2");
System.out.println("这里是子类的有参构造方法 参数为"+s); }
public TestSuper(String s1, String s2)
{
System.out.println("这里是子类的双参构造方法 参数s1= "+s1 +"s2 = "+s2);
}
// static TestSuper aa=new TestSuper();
public static void main(String []args)
{
TestSuper test = new TestSuper(); //不能用static修饰方法体内的变量和对象,因为static是属于类的是全局的
TestSuper test1=new TestSuper("AAA");
TestSuper test2=new TestSuper("AAAA","BBB");
f();
}
}class A {
public A()
{
System.out.println("这里是父类的无参构造方法");
}
public A(String s)
{
System.out.println("这里是父类的有参构造方法 参数为"+s);
}
public A(String s1, String s2)
{
System.out.println("这里是父类的双参构造方法 参数s1= "+s1 +"s2 = "+s2);
}
}
public class TestSuper extends A {
static TestSuper aa=new TestSuper(); //不能在main方法体外new对象,但static 修饰可以。
static void f()
{
TestSuper aaaaa=new TestSuper(); //可以在静态方法内new对象
}
public TestSuper()
{
super("hello");
System.out.println("这里是子类的无参构造方法");
}
public TestSuper(String s)
{
super(s,"parameter2");
System.out.println("这里是子类的有参构造方法 参数为"+s); }
public TestSuper(String s1, String s2)
{
System.out.println("这里是子类的双参构造方法 参数s1= "+s1 +"s2 = "+s2);
}
// static TestSuper aa=new TestSuper();
public static void main(String []args)
{TestSuper aa=new TestSuper();
TestSuper test = new TestSuper(); //不能用static修饰方法体内的变量和对象,因为static是属于类的是全局的
TestSuper test1=new TestSuper("AAA");
TestSuper test2=new TestSuper("AAAA","BBB");
f();
}
}class A {
public A()
{
System.out.println("这里是父类的无参构造方法");
}
public A(String s)
{
System.out.println("这里是父类的有参构造方法 参数为"+s);
}
public A(String s1, String s2)
{
System.out.println("这里是父类的双参构造方法 参数s1= "+s1 +"s2 = "+s2);
}
}
被static修饰的成员变量和成员方法独立于该类的任何对象。也就是说,它不依赖类特定的实例,被类的所有实例共享。只要这个类被加载,Java虚拟机就能根据类名在运行时数据区的方法区内定找到他们。因此,static对象可以在它的任何对象创建之前访问,无需引用任何对象。
加上static:在执行main()方法时,jvm虚拟机发现有static TestSuper aa=new TestSuper(); 这条语句,紧接着就去执行 public TestSuper()
{
super("hello");
System.out.println("这里是子类的无参构造方法");
}
这个方法,然后在运行A类里的public A(String s)
{
System.out.println("这里是父类的有参构造方法 参数为"+s);
}
方法,然后再回头执行main方法里的TestSuper aa=new TestSuper(); 这条语句。所有加上static是结果就是导致了main方法里的TestSuper aa=new TestSuper(); 在static TestSuper aa=new TestSuper();后执行。不会出现死循环的
不加static:
先执行main()里的TestSuper aa=new TestSuper(); 然后在去TestSuper类里执行它的构造方法,但是在运行到TestSuper类时,发现又有一条TestSuper aa=new TestSuper(); 语句,这个才去执行TestSuper类public TestSuper()
{
super("hello");
System.out.println("这里是子类的无参构造方法");
}
的构造方法,所以这个时候main()方法里的TestSuper aa=new TestSuper();(执行TestSuper()的构造方法就被挂起了),这个时候在回来执行main()的TestSuper aa=new TestSuper();这条语句.....
所有这样就又回来了,就成了死循环了,结果内存就溢出了。
不加static运行程序报的错是内存溢出,程序死循环了.....
希望你能看明白
class Test11 {
public Test11 t1 = new Test11();
public Test11 newObject(){
return new Test11();
}public static void main(String[] args)
{ }
}
http://www.lunwenwa.com/qklb.htm