473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

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)
{
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.

thanks for any help,

murat
--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #1
2 1909
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.

Jul 17 '05 #2
> class MyClass
{
MyClass(File f)
{
ObjectInputStream s = new ObjectInputStream(new
FileInputStream(f));
this = (MyClass)s.readObject();
}
}


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;
ObjectInputStream s = new ObjectInputStream(new
FileInputStream(f));
temp= (MyClass)s.readObject();
this.x=temp.x; //member by member
copy for all members
}
}

hope this should help
regards
amey
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: RonHiler | last post by:
My copy constructor is crashing my program, and I can't figure out why. I'll try to make the code listing as short as I can. Here are the two headers: class BreakthroughClass { public:...
10
by: linkspeed | last post by:
Following texts are from C# spec. The optional constructor-initializer specifies another instance constructor to invoke before executing the statements given in the constructor-body of this...
0
by: Efkas | last post by:
I am trying some interresting features to load an assembly on the fly. I read some samples, but what I am trying to do is a little more complex. - First, I have a app.config file containing keys...
4
by: Gustaf Liljegren | last post by:
This may be a question of taste, but if there is a best practice, I want to know. I have two choises: SomeClass s = new SomeClass(file); // Load the file or SomeClass s = new SomeClass();...
6
by: Henry | last post by:
I was trying to derive a class from System.Windows.Forms.ComboBox. My goal was to create a class that loaded its own data. I did not want to create too many objects, so I tried to share a Database...
3
by: Mike Owen | last post by:
I have just tried to open a the file "Controls/CreateEditPost.cs" from within Visual Studio .Net, and the error message: 'An error occurred while loading the document. Fix the error, and then...
1
by: GRiN | last post by:
In VS2003 I have been using an ArrayList loading it with a copy constructor and it seems to work fine. {I am acutally loading a lot of constants first, not one a shown} public ArrayList al;...
3
by: tropos | last post by:
(Platform: Solaris with gmake and native Sun C++ compiler) Problem: If I create a shared object (.so file) and load it into a executable, the loader correctly runs constructors of static...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.