• 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

JTip Question

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<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.
 
Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic