-1

I was wondering if anyone could help me. I am stuck with something that seems simple. Say I initialize a Student object with the values of name, age and address in one class. How can I share THAT INSTANCE with another class, eg enable methods in the second class to make changes to the instance, thereby affecting the first class etc.

I was thinking of using JFrames where a new frame would popup and a button would affect values on the first frame.

Thank you

  • 1
    Take a look at [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) for some reasons why you shouldn't avoid multiple frames... – MadProgrammer May 15 '14 at 00:09

2 Answers2

1

mutators can help you there. or, the singleton pattern, if you want to go "hard-knox". it's a pattern that allows only one single instance of a class in a jvm to exist.

just look at this

Stultuske
  • 9,296
  • 1
  • 25
  • 37
1

in java if you pass reference of an object to a method and do any changes to that object property, it will change the actual object properties(call by reference)

also if you want to have only single object of a class that needs to be shared across other classes create a singleton pattern

dev2d
  • 4,245
  • 3
  • 31
  • 54