File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Accessing an object from different classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing an object from different classes" Watch "Accessing an object from different classes" New topic

Accessing an object from different classes

Daniel Gallant

Joined: Sep 09, 2009
Posts: 27
I have a problem. I'm writing a character creation program and I want to use a single object to represent the character. So far, so good. The problem I'm having is passing that object to and from different JDialogs. I have different JDialogs to print, save, load, select options, etc. Here is part of the CharacterRecord object:

I use the synchronized keyword with all the methods, and the volatile keyword with all the variables.

My problem is that when I call CharacterRecord ch = CharacterRecord.getCharacter(); in my JDialogs, they do not return the curent character, they always return a blank character (i.e. an un populated character record).

It's driving me crazy!!! I know it's something simple and stupid that I missed. Please help!

If you need any additional information, please let me know.

Thanks for your help!
pete reisinger
Ranch Hand

Joined: Dec 30, 2009
Posts: 46
it returns empty character because you are calling static variable ch which is new CharecterRecord()
could you post more of your code? because I can see only this. I cannot see where you set the character record.
Then I don't understand why your method is synchronized - it doesn't access instance variables but static variable, but maybe I'm missing something.
Daniel Gallant

Joined: Sep 09, 2009
Posts: 27
Let me expand on my original question. Here is part of the class that displays the UI, handles user input, etc. I have methods that use the ch object, populate it based on user input, etc. On the main UI, the summary panel populates from the ch object correctly. Any use of the ch object within in the Character_FolioView class works, but any use outside of that class, like in any JDialog, seems to use a new or blank ch object.

At the beginning of the classes, I start with code like this:

And here is the populatePage method:

I agree. Here's the link:
subject: Accessing an object from different classes
It's not a secret anymore!