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

why does value not change???

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output of this program ?

Choose: 1

Options

1.) FunnyMan

2.) seizure

3.) mothulla

4.) NullPointerException

5.) Compile time error

answer is seizure..i expected it to be mothulla...since the set method calls setname method and sets it value to mothulla....
if anybody has understood kindly explain..thanks in advance
 
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
void deleteSharpen(Sharpen sharpen)
{
sharpen = null;
sharpen = new Sharpen();
sharpen.setName("mothulla");
}

In the above function the argument "sharpen" is local to method. It is shadowing the instance variable "sharpen". You are making local variable to NULL. change the argument to any other name as shown below:

void deleteSharpen(Sharpen sharpen1).....you will get output "mothulla"
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic