• 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • 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

 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!