Re: constructor loading it's object from a file
From: Raymond DeCampo (rdecampo_at_hold-the-spam.twcny.rr.com)
Date: 12/05/03
- Next message: Raymond DeCampo: "Re: .equals(o) and Set membership"
- Previous message: Raymond DeCampo: "Re: Getting class name without instance?"
- In reply to: Murat Tasan: "constructor loading it's object from a file"
- Next in thread: Amey Samant: "Re: constructor loading it's object from a file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 05 Dec 2003 02:32:30 GMT
Murat Tasan wrote:
> i'd like to have a constructor that loads an object from disk and returns
> it... kinda like:
>
> class MyClass
> {
> MyClass(File f)
> {
> ObjectInputStream s = new ObjectInputStream(new
> FileInputStream(f));
> this = (MyClass)s.readObject();
> }
> }
>
> now i know you cannot do this because this in the constructor is a final
> variable...
>
> is there trick that i don't know of that can do this?
>
> p.s., before i get the typical responses... i already am using a static
> factory method to accomplish the same thing... i'm just looking to make it
> even easier to use my classes.
>
What makes a constructor "easier" than a static method?
If you really wanted to do this you could read in the object from the
file and then copy all of its instance variables. Sounds like too much
work and too error prone for my taste however.
Ray
P.S. Can't tell if you edited it for posting, but don't forget to close
your streams in a finally clause.
- Next message: Raymond DeCampo: "Re: .equals(o) and Set membership"
- Previous message: Raymond DeCampo: "Re: Getting class name without instance?"
- In reply to: Murat Tasan: "constructor loading it's object from a file"
- Next in thread: Amey Samant: "Re: constructor loading it's object from a file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|