• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about static and instance initializer blocks with inheritance

 
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 tried some code regarding the instance and static initilizer blocks
I came to know that
superclass static blocks are called before the subclass static blocks and also the same with instance block when the object of the subclass is created
but when I created the object of the super class then only instance block of superclass gets called and not static blocks
why is it so?
can someone give me full explanation for that?
step by step control flow?


and here is the output of program

SuperClas static block
subclass static block
Superclass instance block
subclass instance block
Superclass instance block
subclass instance block
Superclass instance block
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static initialization blocks are called when the class is loaded, not in the Object creation time! Do better coding, You'll find out!
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following points :-

1) Static blocks are called when class is loaded by the JVM into memory.
2) Class is loaded by JVM into memory when it is required.....ie when we use it.
3) Once class is loaded , it is there in memory , it is not loaded again and again, hence static blocks are called only once.
4) Init Blocks are called every time you create an instance of a class. ie whenevr you say new on class.

5) When you say new......on Class <Suppose class is already loaded into memory ie static blocks are alredy being run by JVM >

1) Superclass object is created first .
2) Subclass object is created then... and so on so forth

and there is only one rule how Object of a class is created whether it is superclass or it is subclass.

1) Instance variables are created first. They are given default values or explicitly initialized values (if any).
2) Init blocks are called next (If any)
3) Constructors are called next.


Thanks


 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sahil Rally wrote:Consider the following points :-

1) Static blocks are called when class is loaded by the JVM into memory.
2) Class is loaded by JVM into memory when it is required.....ie when we use it.
3) Once class is loaded , it is there in memory , it is not loaded again and again, hence static blocks are called only once.
4) Init Blocks are called every time you create an instance of a class. ie whenevr you say new on class.

5) When you say new......on Class <Suppose class is already loaded into memory ie static blocks are alredy being run by JVM >

1) Superclass object is created first .
2) Subclass object is created then... and so on so forth

and there is only one rule how Object of a class is created whether it is superclass or it is subclass.

1) Instance variables are created first. They are given default values or explicitly initialized values (if any).
2) Init blocks are called next (If any)
3) Constructors are called next.


Thanks




Object Creation criteria:

1. Set fields to default initial values (0, false, null)
2. Call the constructor for the object (but don't execute the body of the constructor yet)
3. Invoke the constructor of the superclass
4. Initialize fields using initializers and initialization blocks
5. Execute the body of the constructor
 
Moguluri Ravi Kiran
Ranch Hand
Posts: 63
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:
Object Creation criteria:

1. Set fields to default initial values (0, false, null)
2. Call the constructor for the object (but don't execute the body of the constructor yet)
3. Invoke the constructor of the superclass
4. Initialize fields using initializers and initialization blocks
5. Execute the body of the constructor


But i think first static bloks,
next static fields &
next instance fields & next what he said.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moguluri Ravi Kiran wrote:
Abimaran Kugathasan wrote:
Object Creation criteria:

1. Set fields to default initial values (0, false, null)
2. Call the constructor for the object (but don't execute the body of the constructor yet)
3. Invoke the constructor of the superclass
4. Initialize fields using initializers and initialization blocks
5. Execute the body of the constructor


But i think first static bloks,
next static fields &
next instance fields & next what he said.



I mean only the Object Creation criteria, not the class loading time stuff.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yoo !!
thank you all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic