Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between instace initializers and the member initializers

 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What do you mean by member initializers.

Best Regards,
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Emanuele.

How are you mate? Preparation?

Good luck,
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Larry,

Perfect answer.

Best Regards,
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Prithvi. Sorry I did not see your good answers before I posted my poor attempts to explain.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean you experts
I'll be joining you within 3 months, atleast in core java
after being SCJP
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasad,

You are still an expert mate.

Happy Preparation,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic