• 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

Constructor

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Process {
byte b=127;

Process() {
this.methodA();
}

void methodA() {
System.out.println("Value of Super b is = " + b );
}

public static void main(String [] args) {
Processor p = new Processor();
}
}
class Processor extends Process {
byte b=126;

Processor() {
System.out.println("Value of b = " + b);
}

void methodA() {
System.out.println("Value of Sub b = " + b);
}
}

The output after run is:
Value of Sub b = 0
Value of b = 126
Why seems methodA of Processor is invoked while b of Processor is not initialized?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm no expert, but this is what I think is going on.
call to new Processor() goes to instantiate a Processor object.
This object instantiation implicitly calls the superclass constructor.
The superclass constructor calls this.MethodA - but the object you are trying to instantiate is the Processor - so it calls the Processor's MethodA - before the Processor object has actually been constructed and instance variables have been assigned.
I added some comments to the code to demonstrate - but I could be wrong. Anyone have a better explanation?
class Process {
byte b=127;
Process() {
System.out.println("Process constructor initialized");
System.out.println("Value of b it " + b);
this.methodA();
System.out.println("Process constructor finished");

}
void methodA() {
System.out.println("Value of Super b is = " + b );
}
public static void main(String [] args) {
Processor p = new Processor();
}
}
class Processor extends Process {
byte b=126;
Processor() {
System.out.println("Processor constructor initialized");

System.out.println("Value of b = " + b);
System.out.println("Processor constructor finished");

}
void methodA() {
System.out.println("Value of Sub b = " + b);
}
}
Output is:
Process constructor initialized
Value of b is 127
Value of sub b = 0
Process constructor finished
Processor constructor initialized
Value of b = 126
Processor constructor finished
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pam,
I also played around with the code and came to the same conclusion. I somehow did not expect the overridden method to be called when the superclass constructor was invoked. and here I was thinking I had grasped this concept quite well.
anyways, good question laoniu!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic