wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JTip Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JTip Question" Watch "JTip Question" New topic
Author

JTip Question

Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
<code><pre>
class Process {
byte b=127;
Process() {
this.methodA();
}
void methodA() {
System.out.println("Process:MethodA() - Value of 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 - Value of b = " + b);
}
void methodA() {
System.out.println("Processor:MethodA() - Value of b = " + this.b);
}
}
</pre>
</code>
Can someone explain why the output is 0 and 126.
Thanks
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242

<code><pre>
class Process {
byte b=127;
Process() {
this.methodA();
}
This method didn't get called
void methodA() {
System.out.println("Process:MethodA() - Value of 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 - Value of b = " + b);
}
Instead this method get called
void methodA() {
System.out.println("Processor:MethodA() - Value of b = " + this.b);
}
}
</pre>
</code>
Can someone explain why the output is 0 and 126.

Since Processor.b didn't get a chance to init thats why output is 0.
Since at runtime the actually Object's method get called thats why Processor method get called.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JTip Question
 
Similar Threads
Jtips #39
A confused result
another strange question
overriding doubt
Constructor