This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why does value not change??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why does value not change???" Watch "why does value not change???" New topic
Author

why does value not change???

maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
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
Lokesh Varma
Greenhorn

Joined: Dec 30, 2009
Posts: 3
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"
 
Don't get me started about those stupid light bulbs.
 
subject: why does value not change???
 
Similar Threads
How to call InnerClass method...Help..Urgent
Problem with BeanUtils
How can I get a run() method to return a string variable, when run() method is required to be void ?
how to accept an instance of a class as a parameter
Clarifications/Suggestions in Java