• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

a confusion question!

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pls read the following code:
class A
{
public A()
{
System.out.println("AAA");
}

System.out.println("456");

}
public class B extends A
{
B()
{
this(12);
System.out.println("BBB");
}
B(int x)
{
System.out.println("CCC");
}

System.out.println("123");

public static void main(String[] args)
{
new B();
}
}
the out is:
456
AAA
123
CCC
BBB

But I think the when the just the time the class is loaded the static method should be execute [the system.out.print() is static method ],as a result, the 123 should be change to after the AAA,I think the out should be :
456
123
AAA
CCC
BBB

I am wrong ,I am sure .but I am confused .I cant find myself on the issue.I will be very glad if somebody clear me .
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the order of initialization:
1)Super class's static blocks and static variable declaration & initialization(as per their order).
2)Sub class's static blocks and static variable declaration & initialization(as per their order).
3)Super class's non-static blocks and non-static variable declaration & initialization(as per their order).
4)Super class's constructor.
5)Sub class's non-static blocks and non-static variable declaration & initialization(as per their order).
6)Sub class's constructor.
The original code will not compile. The following will:
class A
{
public A()
{
System.out.println("AAA");
}
{System.out.println("456");}
}
public class B extends A
{
B()
{
this(12);
System.out.println("BBB");
}
B(int x)
{
System.out.println("CCC");
}
{System.out.println("123");}
public static void main(String[] args)
{
new B();
}
}

[This message has been edited by Peter Java (edited September 11, 2001).]
 
james gong
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the output seems doesnt print out according the order you give.
in my opinion the output should be ;
456
123
AAA
CCC
BBB

 
it's a teeny, tiny, wafer thin ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic