File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Constructor question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Constructor question" Watch "Constructor question" New topic
Author

Constructor question

eric moon
Ranch Hand

Joined: Nov 26, 2000
Posts: 133
I have a utility that reads and writes objects to disk. I have an object Params that gets stored to disk via the utility. Params is a singleton that gets created when the user first starts the program, and initializes the users data. So in the private constructor to Params, I first check whether the file exists on disk, and if it does, I want to use it to create my Params object. I though maybe I could just use "this", but the compiler is complaining. How best to do this?

[This message has been edited by eric moon (edited January 12, 2001).]


<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>"Those who cast the votes decide nothing. Those who count the<BR>votes decide<BR>everything." <BR> -Joseph Stalin<HR></BLOCKQUOTE>
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by eric moon:
I have a utility that reads and writes objects to disk. I have an object Params that gets stored to disk via the utility. Params is a singleton that gets created when the user first starts the program, and initializes the users data. So in the private constructor to Params, I first check whether the file exists on disk, and if it does, I want to use it to create my Params object. I though maybe I could just use "this", but the compiler is complaining. How best to do this?

As you found out you can't assign a new object to "this". Since you are already using a factory method for the singleton pattern, why not put the creation detail, i.e. the choice between deserializing from disk, or creation from scratch, in that factory?

HTH
- Peter
eric moon
Ranch Hand

Joined: Nov 26, 2000
Posts: 133
Oh, that's how I do it?? My class actually contains a single static instance of itself?? Trippy freak-out weird! I'll try it.
Thanks!
e
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor question