• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

java instance creation

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this question in one of the mock exams and was doubtful about the sequence of creation -

class A {
A() {System.out.print("CA ");}
static {System.out.print("SA ");}
}
class B extends A {
B() {System.out.print("CB ");}
static {System.out.print("SB ");}
public static void main (String[] args) {B b = new B();}
}

What is the result of attempting to compile and run the above program?

a. Prints: SA CA SB CB
b. Prints: SA SB CA CB
c. Prints: SB SA CA CB
d. Prints: SB CB SA CA
e. Runtime Exception
f. Compiler Error
g. None of the above


As i understand, the subclass static block is called after the invocation of superclass contructor and I would expect the answer to be (a). But the answer was found to be (b). Any ideas??
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The static initializer blocks are called as the class files are loaded, superclass first. This prints "SA" then SB".

After the class files are loaded, static method main() is called.

Main uses the new operator to create a B object. The B constructor starts with a (hidden) call "super();". The A constructor also starts with "super();". The Object constructor does nothing and returns to the A constructor which prints "CA". The A constructor returns to the B constructor which prints "CB".
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by prafulla shrikhande:
I found this question in one of the mock exams and was doubtful about the sequence of creation -

class A {
A() {System.out.print("CA ");}
static {System.out.print("SA ");}
}
class B extends A {
B() {System.out.print("CB ");}
static {System.out.print("SB ");}
public static void main (String[] args) {B b = new B();}
}

What is the result of attempting to compile and run the above program?

a. Prints: SA CA SB CB
b. Prints: SA SB CA CB
c. Prints: SB SA CA CB
d. Prints: SB CB SA CA
e. Runtime Exception
f. Compiler Error
g. None of the above


As i understand, the subclass static block is called after the invocation of superclass contructor and I would expect the answer to be (a). But the answer was found to be (b). Any ideas??



Well. I suppose u know that when u create the instance of a subclass it calls the constructor of the superclass! In this case there is a static block in the superclass. So this happens: U create the instance of a subclass and then because a static block is executed when the class is loaded; the superclass is first loaded, before the subclass which leads to the execution of thier static blocks in the order of Superclass first then subclass. After that, the superclass's constructor executes before that of the subclass. Hope u do understand.
Saheed.
SCJP 1.4
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to what Mike says: you can remove "B b = new B();" from the main method. You will see that the program still prints SA SB without any objects being created at all.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic