Roel De Nijs wrote:It's really not that hard You'll definitely be ale to spot the actual issue after a (few) pointer(s).
What happens when this statement is executed?
Roel De Nijs wrote:
Mushfiq Mammadov wrote:Default constructor of Emp is called.
True! But is this really the only thing happening?
Mushfiq Mammadov wrote:
Roel De Nijs wrote:
Mushfiq Mammadov wrote:Default constructor of Emp is called.
True! But is this really the only thing happening?
Not of course, sorry, at first instance variable initialize then default constructor is called. But instance variable mgr also call default constructor.
Roel De Nijs wrote: Which code is used to initialize mgr?
Mushfiq Mammadov wrote:I know that the following code is recursion:
Mushfiq Mammadov wrote:I try to relate the above code with this code but I can't find any correspondence.
Paweł Baczyński wrote:
Fields of an object are initialized before constructors are called.
Roel De Nijs wrote:
Mushfiq Mammadov wrote:This new Emp();
True! So you create a new Emp instance again. And what does happen when you create a new Emp instance?
Mushfiq Mammadov wrote:Evrika Probably I can understand! The default constructor is called and the instance variable mgr is initialized. When mgr is initialized this process is repeated and recursion happens Am I correct?
Roel De Nijs wrote: This codeis equivalent to
Gajendra Kangokar wrote:Fields of class are not initialized before constructor call but Constructor initializes the fields of class.
JLS wrote:Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:
1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
2. If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.
5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.
Gajendra Kangokar wrote:
Paweł Baczyński wrote:
Fields of an object are initialized before constructors are called.
Fields of class are not initialized before constructor call but Constructor initializes the fields of class.
Roel De Nijs wrote:
Gajendra Kangokar wrote:
Paweł Baczyński wrote:
Fields of an object are initialized before constructors are called.
Fields of class are not initialized before constructor call but Constructor initializes the fields of class.
True!
If you compile this codeyou'll get this code
Mushfiq Mammadov wrote:That is it which I need. If you write this code at first post everything would be clear. But you gave a chance to me that I found it myself and it was very helpful and enjoyable for me. Thanks a lot, Roel