LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2011, 10:53 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
saving a Serializable object within it's own class (JAVA)


Hi all,
I found out how to save an object using Serializable in JAVA, the standard
Code:
try
         {
            String filename = "yep";
            FileOutputStream fs = new FileOutputStream( filename );
            ObjectOutputStream os = new ObjectOutputStream( fs );
            os.writeObject( MyProgramList.MainVariableList );
            os.close();
            

         }
        catch( Exception ex)
        {
            ex.printStackTrace();
        }
however how can I set up my MainVariableList to have a function/method saveobject so it saves the object within it's own class, the below code compiles, however when I run it reports an error on startup but works, basically what do I use for the red line, how do I tell it what object is passed to the class? Thanks all

Code:
public class GameTextData implements Serializable {
   //fields

    private String namestr;
    blah ....

   //methods
  public void saveobject()
    {
 try
         {
            String filename = "yep";
            FileOutputStream fs = new FileOutputStream( filename );
            ObjectOutputStream os = new ObjectOutputStream( fs );
            os.writeObject( MyProgramList.MainVariableList );
            os.close();
            return( true );

         }
        catch( Exception ex)
        {
            ex.printStackTrace();
            return( false );
        }


    }
 
Old 05-12-2011, 02:57 PM   #2
Hidden Windshield
Member
 
Registered: Jul 2010
Distribution: Fedora
Posts: 68

Rep: Reputation: 27
If I understand you correctly, you want
Code:
os.writeObject( this );
"this" is a special variable in Java that points to the "current object". So, for instance, if you called "x.saveobject()", then, within the method, "this" would point to whatever object "x" points to.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Observer and Serializable manolakis Programming 3 04-12-2009 07:27 AM
Pass object variable to anothor class in JAVA hus Programming 2 05-11-2005 04:04 AM
C++ class-object? shivaligupta Programming 2 01-06-2005 02:25 AM
Java, implementing Serializable djgerbavore Programming 0 10-01-2004 11:31 AM
Event driven object-to-object: C++ template class mecanism ( NOT STL or STDC++) bretzeltux Programming 2 12-23-2003 02:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration