Win a copy of Svelte and Sapper in Action this week in the JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Singletons and Java

 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still new to all the programming stuff, so bare with me.
In the Singleton pattern, you create a private static variable of the class that you want only one object of, and create that object somewhere in the class' methods. However, class variables are akin to global variables, especially in this class, so shouldn't they be avoided? If this is so, how would you create a Singleton and avoid using "global" variables?
Thanks
Jason
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, static variables are global within the limits of their visibility. If you have a private static variable, it is limited in visibility to objects and methods of that class. No other class can access your static object except through methods you define.
------------------
Hope This Helps
Carl Trusiak, SCJP2
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, so making it private, you still keep the OO "feeling" of things.
Would you want to mark any static variable private, so that you aren't focused on data, but instead on the object that utilizes the data?
What would be an example of not making a static variable private? (besides something like System.out )
Jason
[This message has been edited by jason adam (edited August 13, 2001).]
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the biggest use is 'static final' fields which are just plain and simple constants. For example, 'public static final double PI = 3.14;' instead of 'public static doubli getPI() { return 3.14;}'
There is nothing wrong with using static variables just as there is nothing wrong with using non-static ones. You just have to use them in an understandable, clean, and sensible manner to solve a problem.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason, take a look at http://c2.com/cgi/wiki?SingletonsAreEvil and http://c2.com/cgi/wiki?SingletonsAreGood for some very interesting discussions.
In my experience, you should be *very* carefull in using singletons (so I am leaning more to the evil-side - for the moment...). On the other hand, the best way to learn when and how to use them is probably just to try it - but don't forget to reflect on the pros and cons you are experiencing.
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent y'all, thanks a bunch. I'm sure next semester when I start going through OO engineering, and OOAD, I'll have at least a basic grasp of this stuff (hopefully!)
Thanks again!
Jason
 
Legend has it that if you rub the right tiny ad, a genie comes out.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic