Possible Duplicate:
Why are static variables considered evil?
Having studied Object oriented programming and design in college a few years back, i remember lecturers getting pretty angry at people using static variables or static methods, as they describe static to be of a very bad design.
My understanding is this Something of type static, is something that gains global "vision" i.e. it can be accessed by anything.
Is my understanding correct? is there information about the use of static that makes it bad use? and is there a way to avoid using static variables or methods in your code?
btw, i am aware that static for the main method is a pre-requisite to the successful operation of the application.
The main legitimate case for using a static variable, and there is no other way to do it, is the singleton pattern.
An accessor method for a singleton, especially one that uses lazy initialization, requires a static method.
There is nothing "bad" about singletons.
Static variables and static methods are quite a different thing.
Static variables are bad, unless they represent "singleton" of some kind, and if they do, then in OOP it's better to create actual singleton class (especially if you can use enum singleton). Among other things, static variables make multi-threaded programming difficult, and can do that even in single-threaded programming, where you "unexpectedly" need two instances of class with static fields.
On the other hand static methods are generally just fine, as long as they do not access any static data, but operate only on their arguments. Of course, if you notice you have
static void MyStaticUtils.operateOnFoo(Foo foo)
, then it's much better to have non-staticvoid Foo.operate()
method. But sometimes you don't have the luxury of adding methods to an existing class, and must operate on instances returned by existing methods, and then static utility methods are definitely good choice.static mainly used for to accessing class members without creating class instance. As you know that why we are writing
public static void main()
just bcoz at the run time jvm can access this method without creating the class object and start up your program.Its depending on your requirement that how you used or say how to declaring member of class using access specifier as private, public, default and protect for access level while the static was for make that as static with access level.
Without static you must declare your member as public or if you inherit your class then make as protected also. As in the same package you define the class then just static for inheriting the class. And for using without inhert class you need to create object of that class and then you can access there members (Note it based on access specifier)
here is the link below for more detail
http://xahlee.info/java-a-day/access_specifiers.html
http://www.javabeginner.com/learn-java/introduction-to-java-access-modifiers
There's nothing wrong with static variables or methods. It's just that with OOP you want to associate as much behavior as possible with objects, and that means using instance methods and variables. Having said that, there are specific cases where static members are necessary: Constructors (as you mention) and the Singleton pattern are good examples.