• 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

this.changeLabel

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Look at the code below-
import java.awt.*;
class changeLabelThruThis {
public changeLabelThruThis(){
Button b = new Button("Before");
this.changeLabel(b);
System.out.println(b.getLabel());
}
public static void main(String [] arg){
changeLabelThruThis mc = new changeLabelThruThis();
MyClass mc1 = new MyClass(); // <== this line
}
public void changeLabel(Button c){
c.setLabel("After");
}
}

Can anyone sugget why the output is "After" even if the b reference is set to "Before" and why the highlighted line in not flagging any error.
please reply urgently.
Nisheeth
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Nisheeth,
You have a file name called "MyClass.java" in your current directory. That is the reason why you are getting no compile time or runtime errors. So, just remove that statement from your code or rename the already existing java file "MyClass.java" from your current directory, to something else if you intend to do anything with your "MyClass".
The rest of the thing is understood. As you have a method which is changing the label. If you still have any doubts give print statements where ever you have doubt. That should clear your doubts.
If you are still not clear, let the ranchers know. They will get it done.

Originally posted by Nisheeth Kaushal:
Hi all,
Look at the code below-
import java.awt.*;
class changeLabelThruThis {
public changeLabelThruThis(){
Button b = new Button("Before");
this.changeLabel(b);
System.out.println(b.getLabel());
}
public static void main(String [] arg){
changeLabelThruThis mc = new changeLabelThruThis();
MyClass mc1 = new MyClass(); // <== this line
}
public void changeLabel(Button c){
c.setLabel("After");
}
}

Can anyone sugget why the output is "After" even if the b reference is set to "Before" and why the highlighted line in not flagging any error.
please reply urgently.
Nisheeth


 
We must storm this mad man's lab and destroy his villanous bomb! Are you with me tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic