• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

instantiation?

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Just {
int a = 10 ;

Just ( ) {
call ( ) ;
}

void call ( ) {
System . out . print( "a = " + a + " ") ;
}
};

class Q05 extends Just {
int b = 16 ;

Q05 ( ) {
call ( ) ;
}

void call ( ) {
System.out.print( " b = " + b + " " ) ;
}
public static void main ( String args [ ] ) {
new Q05 ( ) ;
}
}


can u plz tell me "how" the above code is printing b=0,b=16 when run?
thanks in advance
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The superclass constructor is called before the instance vars are initilized with initilizer values. Section 12.5 of the JLS explains the steps for creating a new object.

First the space is allocated for all instance variables and they are all initialized to their default value. Then the constructors are called as follows:

1) assign parameter values to the constructor params
2) call any this() ctor and recursively do these 5 steps
3) in the absense of a this() ctor call the superclass ctor (and do these steps there)
4) apply any instance initilizers
5) execute the rest of the ctor body

If you notice the superclass ctor is called (step 3) before the instance variable initializers are applied (step 4).

The example code you supplied shows this. When creating a Q05 object the following happens.

- space for instance variables a and b is allocated and initilized to 0 (int default)
- the Just() ctor is called (superclass of Q05) and in this body it calls the call() method
- since the class being instantiated is Q05 the Q05 call() method is called. This method prints the value of the variable b which is still 0.
- once the Just() ctor finished the Q05 initializers are applied which results in b getting the value 16.
- then the call method is called from the Q05 ctor which results in 16 being printed.

Hope that all makes sense. The trick here is that the variable initializers are not applied until AFTER the super ctor is executed.

_M_
 
achayya matta
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for ur wonderful explanation
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic