• Post Reply Bookmark Topic Watch Topic
  • New Topic

ObjectOutputStream ObjectInputStream  RSS feed

 
Ian Looney
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, Hoping somebody can help me out with this.
I have a class with static ArrayLists to hold objects such as Members,Players etc.
I want to save the class with the arrays so as to reload them again and hold onto the list of objects within those ArrayLists.

The ArrayClass


When I close the JFrame I execute this code to save the object toa file

THis code saves the file ok but i cant tell whats in the file
This is the code i execute to load the file when the JFrame opens

The arrays within the ArrayClass are empty when i reload the application.
I cant tell if the arrays are being properly saved or is it in the reloading from file???
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static members of a class aren't serialized when you write an object of that class to an ObjectOutputStream.

However there's absolutely no reason why those members should be static, so the simplest thing is to just not declare them as static.
 
Ian Looney
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Paul,
I had them as static as i want to be able to add to them and access them all over the program.
different classes will be adding to the arrays and reading from them, I thought having them static would achieve this.
if i instantiated another class with the arrays as instance variables before saving and the opposite when loading??
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I see. Then you haven't started to write code which uses objects yet. In which case, trying to write code which serializes objects would be getting ahead of yourself a bit. Are you learning Java on your own or is there somebody else directing what you learn?
 
Ian Looney
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the OCJP done but to be honest I havent a lot of actual code writing experience.
I got the serializing sorted. I am using the arrays to hold objects so i can access them everywhere.
I used a separate class to instatiate an object copying the arrays basically then saved that object.
Its working fine for me, any suggestions on a better approach would be appreciated.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!