wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes mismatch in printing the value of a variable 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 » Java » Beginning Java
Bookmark "mismatch in printing the value of a variable " Watch "mismatch in printing the value of a variable " New topic
Author

mismatch in printing the value of a variable

Rajat Bhatnagar
Greenhorn

Joined: Mar 11, 2008
Posts: 22
i have a simple class Gertude in which i have a private variable number of type int.

I access this variable by getter and setter methods.

initially i set the value of this number to 100 by saying gerry.setNumber(100);and print it back by System.out.println(gerry.getNumber()) where gerry is a Gerude object.

Everything runs fine till here

But When I try printing the value of this gertude variable
by saying (new Gertude()).getNumber() it always comes to 0

I cant understand as to why this mismatch is happening

My code is



Regards<br />Rajat Bhatnagar<br /><a href="http://guideofgreatness.googlepages.com" target="_blank" rel="nofollow">http://guideofgreatness.googlepages.com</a>
saurav sarkar
Ranch Hand

Joined: Jan 07, 2007
Posts: 180

what is your question?.
Both the objects are different.Your second SOP will print '0' always
as the instance variable of type int is initialized default to 0.


Be Objectively Oriented.Explore the power of OOPs.
My Blog, Eclipse EMF Query committer.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Rajat Bhatnagar:
... But When I try printing the value of this gertude variable
by saying (new Gertude()).getNumber() it always comes to 0...

You're creating a new instance of Gertude, and then calling getNumber() before setting the number to anything, so number has its default of 0.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Rajat Bhatnagar
Greenhorn

Joined: Mar 11, 2008
Posts: 22
It still doesn't work..

Even when i set the value by saying (new Gertude()).setNumber(200);
before the SOP it still prints the value as 0

saurav sarkar
Ranch Hand

Joined: Jan 07, 2007
Posts: 180

again you are creating two instances of your class.
calling new Gertrude() everytime creates a new instance for you.
Rajat Bhatnagar
Greenhorn

Joined: Mar 11, 2008
Posts: 22
I got it ..

thnks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Gertrude g = new Gertude();
g.setNumber(200);
System.out.println("Value of g.getNumber() is "+ g.getNumber());


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: mismatch in printing the value of a variable
 
Similar Threads
nikos thread question
Is this thread safe? Simple question.
getting and showing a wrapped value.
Question about instance variables
Tournament Tree help