Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Doubt in Singelton class

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

in singeltoon classes we declare a method

public <classname_as Return aType> getInstance ()

{
// checking for null etc

// finally we return the instance object
return instance;

}

The instance object has an instance of its class (eg say Math class). This instance is called from a method(say X)/class(say A).

The method(X)/class(A) can used access the private members of the Math class as the object reference is copied. Doesn't this defeat the data secuity of the Math class?? I mean then there is no use in keeping the variables of Math class private as any class can get the instance of Math class by calling the public method Math.getInstance()

Is this true or am i missing something?
 
Marshal
Posts: 75638
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am afraid I am finding your question hard to understand.
Have you created a Math class? You can get no end of confusion if you create classes with the same name as classes in the java.lang package.
The Math class has no data in it, with the possible exception of the two constants PI and E. And there are no instances of the Math class; it is uninstantiable.

The getInstance() method in a singleton class should be declared static.
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i have a lecture right now. will reframe my question with specific details aftr 3 hrs. Sorry to make it obtruse
 
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read
Singleton considered stupid yet?
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton Pat for providing such a wonderful link
 
Lookout! Runaway whale! Hide behind this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic