2013年9月25日星期三

Find a Java stuck , are interested in. Look

I found a Java trap , although I know that this is why, however, CSDN group in the next release , interested to answer ,
correct answers to the points :
public class A {
private A a = new A ();
public A () {
System.out.println (" Hello ! " ) ;
}
}
/ / test class
public class test {
public static void main (String [] args) {
A s = new A ();
}
}

Will 1, the program what is the result ? 2, why ?
------ Solution ---------------------------------------- ----
What is the trap is completely object instantiation of the class does not understand due process , the object is instantiated , it will first initialize all member variables , of course, if there is , then the parent class , parent class the members will first initialize the member variables and then initialize itself , if the member variable initialization method is called , then it will also call its methods , followed by members of the block of code is executed and then the constructor ( if the parent class also perform the parent class 's constructor ) .
put the landlord here is the new A run sequence when initialized inside the member variables a, it has conducted a instantiation of A , will come again then initialize A, is the same initialize member variables a, but they were a instantiation of a , will come again then initialize a. So infinite recursion anymore
------ For reference only -------------------------------- -------
you mean unlimited create an A object, and then stackOverFlow up ? I do not see strange place , unlimited calling private A a = new A ();
------ For reference only ------------- --------------------------

class A{
 A a = null;
public A() throws InterruptedException{
Thread.sleep(1000);
System.out.println("-----");
a = new A();
System.out.println("hello");
}
}

public class Test {
public static void main(String[] args) throws InterruptedException {
A a = new A();
}
}

changed that. Member variable initialization assignment is equivalent to an assignment in the constructor method . Constructor calls the constructor method constantly , became an endless loop , and then stack overflow. .
------ For reference only -------------------------------------- -
indeed it is, tried it really wrong. But this is also the case in other programming languages ​​illnesses it
------ For reference only --------------------------- ------------
traps, pits dug your own bar
------ For reference only --------------- ------------------------
go good to see the face of the object design pattern right, a single-case model , they are written well, but also digging himself buried.
------ For reference only -------------------------------------- -
constructor infinite recursion .
------ For reference only -------------------------------------- -
this is not a trap , you are digging their own pit jump
------ For reference only --------------------- ------------------


That's right ...
------ For reference only ----------------------- ----------------


brothers , I know that you are powerful !
I can tell you I was deliberately written so what ?
If you really have a very strong , this simple question should be a small case, then I ask , why ?
------ For reference only -------------------------------------- -


really my own digging ! ! !
------ For reference only -------------------------------------- -


correct ! ! !
------ For reference only -------------------------------------- -

question is answered you out .
However, as the questioner , I propose a different conclusion with you : VM in treatment, there follows , see
Compiled from "test.java"
public class test extends java.lang.Object {
public test ();
Code:
0: aload_0
1: invokespecial # 1; / / Method java / lang / Object. "": () V
4: return

public static void main (java.lang.String []);
Code:
0: new # 2; / / class Singleton
3: dup
4: invokespecial # 3; / / Method Singleton. "": () V
7: astore_1
8: return

}
execution to astore_1 when a pop-up from the top of the stack reference type , or a reference to the address of the pop-up , and then stored in the specified index is a local variable , and is about to begin , refer to the local . So infinite recursion will occur , but can not enter the building blocks inside, not the end of the structure ! So seemingly simple constructor , but because of this, enter an infinite loop ! Many people say that enters an infinite recursion, I do not agree , personal opinion, because there is no access inside the function , simply load it, how can recursion ? Is a cycle of death !

没有评论:

发表评论