tags:

views:

287

answers:

3

How can we distinguish to create a class which is static?

+2  A: 

A static class forces all of its methods to be static and prohibits an instance constructor therefor can't be instantiated. If your question extends to WHEN to use static and WHEN instance, please do a search on StackOverflow (or check out the Related box on this page)

Zyphrax
A: 

At least in C#, static classes and class members are used to create data and functions that can be accessed without creating an instance of the class.

Otávio Décio
A: 

If you want the class to be static in nature i.e. have only 1 copy within the program (VM) then there are two obvious mechanisms: 1. Make all members and methods of the class static (Java/C#). 2. Use Singleton design pattern.

For this case (static in nature), we don't have a language construct and hence one of the above technique is used. As to your question for this case, such classes should be created if you want your functionality to be accessible globally, unchanged and instantly accessible e.g. utility methods, global constants etc.

Secondly, the keyword 'static' is used with classes to increase their visibility in the package. This keyword can only be applied on inner classes and allows the access to inner classes without the context of their parent class. Such kind of static classes should be used only for those inner classes that serve their purpose within the parent class as well as are useful outside the class or the package e.g. Key of a POJO.

Monis Iqbal