-
October 16th, 2016, 10:43 AM
#1
Sequence in which Non Static Members get loaded in Java
Hi All,
I was brushing up my Java concept and faced a scenario and bit confused about it.
I made 2 classes ClassB and ClassA extends ClassB.
Below is the code
Code:
public class ClassB {
ClassB() {
System.out.println("Constructor Class B");
}
{
System.out.println("IIB class B");
}
static {
System.out.println("SIB Class B");
}
}
public class ClassA extends ClassB {
ClassA() {
System.out.println("Constructor Class A");
}
{
System.out.println("IIB class A");
}
static {
System.out.println("SIB Class A");
}
public static void main(String[] args) {
new ClassA();
}
}
OUPUT
SIB Class B
SIB Class A
IIB class B
Constructor Class B
IIB class A
Constructor Class A
I understand how the Static Initialization block works, but confused about the non static one.
According to my understanding, when the object gets created IIB of Parent then IIB of Child should print then the constructor.
Output according to my understanding:
SIB Class B
SIB Class A
IIB class B
IIB class A
Constructor Class B
Constructor Class A
Can anyone spot some more light on this
Last edited by 2kaud; October 17th, 2016 at 01:32 AM.
Reason: Added code tags
-
October 16th, 2016, 06:33 PM
#2
Re: Sequence in which Non Static Members get loaded in Java
The code would be easier to read if it were properly formatted and wrapped in code tags.
Code without formatting and indentations is hard to read and understand.
Norm
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|