• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic