constructor loading it's object from a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Murat Tasan

    #1

    constructor loading it's object from a file

    i'd like to have a constructor that loads an object from disk and returns
    it... kinda like:

    class MyClass
    {
    MyClass(File f)
    {
    ObjectInputStre am s = new ObjectInputStre am(new
    FileInputStream (f));
    this = (MyClass)s.read Object();
    }
    }

    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.

    thanks for any help,

    murat


    --
    Murat Tasan
    mxt6@po.cwru.ed u
    tasan@eecs.cwru .edu
    murat.tasan@cwr u.edu
    Learn how the Genomics Core at Case Western Reserve University can provide you with a wide range of genomics and DNA/RNA quality control services.


  • Raymond DeCampo

    #2
    Re: constructor loading it's object from a file

    Murat Tasan wrote:[color=blue]
    > i'd like to have a constructor that loads an object from disk and returns
    > it... kinda like:
    >
    > class MyClass
    > {
    > MyClass(File f)
    > {
    > ObjectInputStre am s = new ObjectInputStre am(new
    > FileInputStream (f));
    > this = (MyClass)s.read Object();
    > }
    > }
    >
    > 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.
    >[/color]

    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.

    Comment

    • Amey Samant

      #3
      Re: constructor loading it's object from a file

      > class MyClass[color=blue]
      > {
      > MyClass(File f)
      > {
      > ObjectInputStre am s = new ObjectInputStre am(new
      > FileInputStream (f));
      > this = (MyClass)s.read Object();
      > }
      > }
      >[/color]

      how about instead of reading your object into this , read it in some
      temporary local object variable & then perform member by member copy
      into this
      something of this sort

      class MyClass
      {
      int x;
      MyClass(File f)
      {
      MyClass temp;
      ObjectInputStre am s = new ObjectInputStre am(new
      FileInputStream (f));
      temp= (MyClass)s.read Object();
      this.x=temp.x; //member by member
      copy for all members
      }
      }



      hope this should help
      regards
      amey

      Comment

      Working...