wood burning stoves 2.0*
The moose likes Java in General and the fly likes Help with getters and setters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help with getters and setters" Watch "Help with getters and setters" New topic
Author

Help with getters and setters

Joe Deluca
Greenhorn

Joined: Jan 29, 2009
Posts: 14
Hey all, please consider the situation:

I have 1 class that stores customer info with getters and setters
I would like access the customer class with a GUI class and pump data into it by creating an instance of the customer class (works fine up to here)
Now i have a third GUI class that i would like to retrieve my data from.

The problem is when i make an instance of the customer class in my second GUI class, it resets the data customer class!!

Any help would be greatly appreciated


SCJP 6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Welcome to JavaRanch

Please supply more details; I don't think we can work out what is going wrong from your brief description.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

If you create a new instance, then of course it doesn't keep the old settings - those are instance specific.

You should either pass the reference to the object, and use the very same object in both GUI classes, or create a copy of the instance. This means you have to copy all fields as well. The easiest way is to make the class Cloneable, or provide a copy constructor:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joe Deluca
Greenhorn

Joined: Jan 29, 2009
Posts: 14
Well so far I have this,


The test class is not getting the same value as Main is, and I'm a bit confused on the copy constructor part..

Thanks for the help
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Joe,

The idea is that when you create a "Test" object, you physically hand it a CustomerInformation object to use; then both Main and Test are holding onto the same CustomerInformation object:





[Jess in Action][AskingGoodQuestions]
Joe Deluca
Greenhorn

Joined: Jan 29, 2009
Posts: 14
ah nice, this has been bugging me for a while!

Thanks again

Joe
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help with getters and setters