*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between instace initializers and the  member initializers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between instace initializers and the  member initializers" Watch "Difference between instace initializers and the  member initializers" New topic
Author

Difference between instace initializers and the member initializers

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

Please tell me the difference between the instance initializers and the member initializers of the class
can you provide a good example for that?
please help


SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

what do you mean by *member* initializers ? you mean field initialization ? instance initializers get execute after the super class constructor completed and before the class constructor complete execution
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hi,

What do you mean by member initializers.

Best Regards,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

even I am confused about those words
I read the post in the blog
here
after reading this I am confused about those words
can you please check it and tell the answer?
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
I read the code and I think Corey, the blogger, created the names of "method initializer" and "instance initializer" for the purpose of the printouts. His point was to demonstrate the different sequence of outputs from the Parent class and the Child class (Choice B in his blog).


SCJP 6
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1


what I wanted to ask is following
1.how the method getA() and getB() get called, I mean we are not using any object for that and also they are not static methods
2.Also the block containing the System.out.println("Parent class instance initializer ") statement is an initialization block isn't it? then how does it get called even if it is not static?
3.Is it related to the object creation? but I don't think so because it does not come with the constructor tree isn't it? I'm really confused.Please help in this matter
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello,

1- Why do you think getA() and getB() can't be called?
2- The initializer blocks run in this order. First static initializers then the instance initializers. Only difference is that, static initializers are called
only once when the JVM loads the class and instance initializers are called everytime an object is created. The order does matter.


Here object is getting created. child class constructor will be called which will implicitly call the parent constructor and thats why initializers are getting called
because you are creating an object in fact. If you no object is created, then instance initializers won't run.

Order is like this

1- Child class constructor got called.
2- Implicit call to super(). Parent class constructor called. First it's instance initializer will run. Member initializer. Then Parent class constructor completes.
3- Same sequence with Child class as well.


Provide a little more details, means elaborate where you are exactly getting confused so that i can answer with more clarity.

Hope this helps,
Emanuele Ghe
Ranch Hand

Joined: Feb 04, 2009
Posts: 111
Hi,
I did some tests with the code you inserted, and I can say that "member initializers" are treated as instance initializer.



If you run the above code, you get



If you make this change to the code:



and run it, you get:


So, "member initializers" are called before/after "instance initializers" based on their relative positions in the code.


SCJP6 with score 90%. I am conscious of my ignorance and ready to learn from everyone.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
I agree with Emanuele.

How are you mate? Preparation?

Good luck,
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Prasad Kharkar wrote:
what I wanted to ask is following
1.how the method getA() and getB() get called, I mean we are not using any object for that and also they are not static methods
2.Also the block containing the System.out.println("Parent class instance initializer ") statement is an initialization block isn't it? then how does it get called even if it is not static?
3.Is it related to the object creation? but I don't think so because it does not come with the constructor tree isn't it? I'm really confused.Please help in this matter


OK, those are different questions.
As Corey explained, those methods get called as part of the object creation (but before the execution of the constructors):
...However, prior to execution of the constructor, we execute instance initializers and member initializers - in order, from top down. That means that the next line we are going to execute is:

int a = getA();


"static" has nothing to do with the calling of getA() and getB() methods. The execution of any initializers and the constructor are the natural course of events during object creation.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Larry,

Perfect answer.

Best Regards,
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Thanks, Prithvi. Sorry I did not see your good answers before I posted my poor attempts to explain.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

yes
I got most of it
but still,
look
here the getA() is the method and how does it get called without even calling it by an object ?
or is it that the members of the class can call the other members without object?
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Prasad Kharkar wrote: ... Here the getA() is the method and how does it get called without even calling it by an object ?


Here is the 2 step process of Parent class becoming an object. (I took out the print code just for simplification.)
The getA() method is being called on line #3. (Of course, you do know that methods can be called from within a class.)
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

yes thank you all
for those valuable answers
Its really great being on this community and getting help from you people
have a very very nice time to all
hope I'll be joining you soon
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Prasad Kharkar wrote:... you people


You are welcomed. Good luck with the rest of Java.

What do you mean, "you people"? (from American film, Tropic Thunder)
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

I mean you experts
I'll be joining you within 3 months, atleast in core java
after being SCJP
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Prasad,

You are still an expert mate.

Happy Preparation,
 
 
subject: Difference between instace initializers and the member initializers