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

mismatch in printing the value of a variable

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

 
Ranch Hand
Posts: 180
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rajat Bhatnagar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
again you are creating two instances of your class.
calling new Gertrude() everytime creates a new instance for you.
 
Rajat Bhatnagar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it ..

thnks
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gertrude g = new Gertude();
g.setNumber(200);
System.out.println("Value of g.getNumber() is "+ g.getNumber());
 
Is this the real life? Is this just fantasy? Is this a tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic