This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Help with getters and setters  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:)
 
Marshal
Posts: 62873
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Please supply more details; I don't think we can work out what is going wrong from your brief description.
 
Sheriff
Posts: 21603
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Joe Deluca
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24203
43
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:



 
Joe Deluca
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah nice, this has been bugging me for a while!

Thanks again

Joe
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!