Win a copy of AWS Security this week in the Cloud/Virtualization forum!
  • 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
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

a confusion question!

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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

 
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic