Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between a singleton and a class having all methods static

 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to the above difference, i would like to know the following :

1) What is class loading? When does it occure for a class that is executed using say a console using the java <classname> command.
This question is important as i am trying to find answers to many doubts related to static initializer blocks.
2) Can you start the execution of a java class without even a single main method.

Thanks in advance - Happy 2010.
 
Kaustav Ganguly
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singleton is used when only one instance of the class should be initialised, typical example would be to maintain cache.

Now the same could be acheived by using a class with all methods as static but the problem in this case is nothing stops you from creating multiple instances of the non singleton class.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A singleton can implement an interface, or extend a class, and can be used wherever that interface / class can be used. A very quick example:
You can then use this to find all sub directories of a file:


Mustafa Garhi wrote:1) What is class loading?

The loading of the Class inside the JVM.

When does it occure for a class that is executed using say a console using the java <classname> command.

In short, whenever it is needed and not sooner. Elaborating, whenever an instance is created, or a static member is accessed, whichever comes first. That includes the main method, so when you type in "java MyClass" the JVM will first load the MyClass class, then invoke its main method.

2) Can you start the execution of a java class without even a single main method.

You can through static initializers:
Now that you have seen how to it, ignore you have ever seen it. You shouldn't use hacks like these; the main method is the official way and the main method is what you should use.
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class with all static members can have a private constructor which is never called locally, so no instances are created at all.
 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to say Campbell?
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a class with all static members (eg java.lang.Math) you may never need to create an object of that class.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was an answer to this:
Kaustav Ganguly wrote:Now the same could be acheived by using a class with all methods as static but the problem in this case is nothing stops you from creating multiple instances of the non singleton class.

Campbell said that a private constructor will stop you from creating multiple instances.
 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So for a class with all methods static and having a private constructor as compared to a singleton has only one major diference - it cannot be used as subclass or a class that implements an interface because it could never be instantiated untill extended. A singleton on the other hand as pointed out earlier could extend and provide implementations to instance methods of the base class or interface.
 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just realised that for a singleton we at least create the instance once - without which it would be impossible to access instance members right?
For a class with all static members we would never ever have to create an instance.
Does this mean we are saving some memory when it comes to a class with all static members and a private constructor because the instance would never be created - i guess not. Please elaborate.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're correct on both accounts. Just one note about "until extended" - if a class only has private constructors it cannot be extended because the constructor chaining will no longer be possible.
 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly - that was silly - i guess i need some more brush up. Thanks man.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic