new 的时候是不是要给类中定义的变量分配内存,而其中有个变量的值是通过new 自己来赋值,那这个new事不是又要分配内存了呢?以此循环。内存是在new的时候分配的把
java单例类设计模式要求自己创建自己
public class Test{ private Test = new Test(); public Test(){ } } 这个没问题:) public class Test{ public Test()[ Test(); } } 编译会出错,因为constructor不能调用自己。
回yushui719(拂晓): main只是一个静态函数,在main中使用new 并不是在类本身使用new,因为main压根就不是这个类的成员回复 kyland(who regisitered my raynix!!!!) : new 方法除了为对象分配一个空间之外,还会执行该类中的构造函数,一般在这里是做一些初始化的动作。 如果在 构造函数中又new了类本身的话,则会造成构造函数的无休止递规。 所以,你的第一种情况是合法的,第二种情况则是不合法的。
public class Test{ private Test = new Test(); public Test(){ } } 这个没问题:) 为什么会没有问题,如果我在别的地方有这样的代码 : public Test t=new Test();新建Test对象,分配private Test内存时有要新建Test对象,这样不要循环吗?
当然可以!你看主函数里的那个PUBLIC类定义一定是在自己的类里面
public class Test{ private Test = new Test(); public Test(){ } } 这个没问题:) 为什么会没有问题,如果我在别的地方有这样的代码 : public Test t=new Test();新建Test对象,分配private Test内存时有要新建Test对象,这样不要循环吗? ------------------------------ 这个当然是可以的啊,不然链表怎么写。
事实胜于雄辩:) 1). public class SelfCallTest{ private SelfCallTest sct = new SelfCallTest(); public static void main(String[] args){ SelfCallTest sct = new SelfCallTest(); } }编译通过,执行时抛出java.lang.StackOverflowError.2). public class Node{ private Node next; private int data; public Node(Node next,int data){ this.next = next; this.data = data; }
public static void main(String[] args){ Node head = new Node(null,1); Node n1 = new Node(head,2); } } 编译执行均OK ===>事实上,这是一个对Java中类数据初始化的理解问题.具体可参考TIJ一书~
Singleton 设计模式就是自己 new 自己,其 构造函数为 Private,像这样public class Singleton { private Singleton() { } public static Singleton getInstance() { return new Singleton(); } }public class Main() { public static void main(String[] args) { Singleton instance = Singleton.getInstance(); //Singleton instance = new Singleton(); 编译不会过,因为无权访问private 方法 } }
我来个最简单的。public class Test{ public final static Test t = new Test(); private Test(){} }
楼上的,你要的是这样的么???(Singleton模式) public class test{ private static test t = new test();//外部不能调用私有成员变量,它是static的,所有test的实例共享这个
public static test getInstance(){ return t ;}//这个方法获取test的唯一实例}
当然可以啊 任何一本JAVA教材都在CLASS内部NEW自己啊 public static void main(String args[]){ //这里肯定有NEW自己的类对象 }
有可能造成死循环,比如 public class A{ public A(){ A a=new A(); } } 当你试图new一个A的时候 A another=new A(); 会出错:java.lang.StackOverflowError 就是说,在构造器内部不能直接或者间接的使用自己同一个构造器
private Test = new Test();
public Test(){
}
}
这个没问题:)
public class Test{
public Test()[
Test();
}
}
编译会出错,因为constructor不能调用自己。
main只是一个静态函数,在main中使用new 并不是在类本身使用new,因为main压根就不是这个类的成员回复 kyland(who regisitered my raynix!!!!) :
new 方法除了为对象分配一个空间之外,还会执行该类中的构造函数,一般在这里是做一些初始化的动作。
如果在 构造函数中又new了类本身的话,则会造成构造函数的无休止递规。
所以,你的第一种情况是合法的,第二种情况则是不合法的。
private Test = new Test();
public Test(){
}
}
这个没问题:)
为什么会没有问题,如果我在别的地方有这样的代码 :
public Test t=new Test();新建Test对象,分配private Test内存时有要新建Test对象,这样不要循环吗?
private Test = new Test();
public Test(){
}
}
这个没问题:)
为什么会没有问题,如果我在别的地方有这样的代码 :
public Test t=new Test();新建Test对象,分配private Test内存时有要新建Test对象,这样不要循环吗?
------------------------------
这个当然是可以的啊,不然链表怎么写。
1).
public class SelfCallTest{
private SelfCallTest sct = new SelfCallTest(); public static void main(String[] args){
SelfCallTest sct = new SelfCallTest();
}
}编译通过,执行时抛出java.lang.StackOverflowError.2).
public class Node{
private Node next;
private int data;
public Node(Node next,int data){
this.next = next;
this.data = data;
}
public static void main(String[] args){
Node head = new Node(null,1);
Node n1 = new Node(head,2);
}
}
编译执行均OK
===>事实上,这是一个对Java中类数据初始化的理解问题.具体可参考TIJ一书~
} public static Singleton getInstance() {
return new Singleton();
}
}public class Main() {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
//Singleton instance = new Singleton(); 编译不会过,因为无权访问private 方法
}
}
public final static Test t = new Test();
private Test(){}
}
但你可以加条件
就变成了sigleton
你写的不是Singleton模式;-(
Singleton模式对这个类只会产生一个实例,但是按照你的写法,只要我不断地调用getInstance就会获得不同的对象.
public class test{
private static test t = new test();//外部不能调用私有成员变量,它是static的,所有test的实例共享这个
public static test getInstance(){ return t ;}//这个方法获取test的唯一实例}
任何一本JAVA教材都在CLASS内部NEW自己啊
public static void main(String args[]){
//这里肯定有NEW自己的类对象
}
public class A{
public A(){
A a=new A();
}
}
当你试图new一个A的时候
A another=new A();
会出错:java.lang.StackOverflowError
就是说,在构造器内部不能直接或者间接的使用自己同一个构造器