aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about static and instance initializer blocks with inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about static and instance initializer blocks with inheritance" Watch "about static and instance initializer blocks with inheritance" New topic
Author

about static and instance initializer blocks with inheritance

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

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


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

Joined: Nov 04, 2009
Posts: 2066

Static initialization blocks are called when the class is loaded, not in the Object creation time! Do better coding, You'll find out!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
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




SCJP 6.0 96%

(Connecting the Dots ....)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Apr 16, 2010
Posts: 62

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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Mar 07, 2010
Posts: 446
    
    1

yoo !!
thank you all
 
 
subject: about static and instance initializer blocks with inheritance