This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Difference between a singleton and a class having all methods static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference between a singleton and a class having all methods static" Watch "Difference between a singleton and a class having all methods static" New topic
Author

Difference between a singleton and a class having all methods static

Mustafa Garhi
Ranch Hand

Joined: Nov 05, 2008
Posts: 111
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

Joined: Dec 14, 2009
Posts: 28
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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

Joined: Nov 05, 2008
Posts: 111
What are you trying to say Campbell?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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

Joined: Nov 05, 2008
Posts: 111
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

Joined: Nov 05, 2008
Posts: 111
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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

Joined: Nov 05, 2008
Posts: 111
Exactly - that was silly - i guess i need some more brush up. Thanks man.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between a singleton and a class having all methods static
 
Similar Threads
Labeled statements
singleton vs static
an interview question
Static Methods and Instance Methods
newInstance()