• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructor question

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic