A static method or variable are members of the class, not objects that might be created with that class.
They can be accessed from an object of that class easily, but a static method can not directly access non-static variables, nor call non-static methods, unless it has an object reference.
A static variable has the same value for all objects of that class. If one object changes the value, then that change affects all the objects, because that static variable only exist s in one place. Compare this with a non-static variable. The variable can change in one object but not the others because each objects has its own variable value in memory.
Take a look at the Math class. All methods and members are static. The way you access them is to call them using Math. ie Math.cos() or Math.PI. If you had to create an object of math first, and the methods were not static you would need to do something like Math math = new Math(), then math.cos().
Generally static variables and methods are used when it makes no sense to have an object of a class(ie the Math class) or when the static variables are going to be the same for all objects. Static methods are used when the method shouldn't be part of an object, which is subjective and subject to controversy. Often it is used to return an object that matches the class type, and has no use of an instance of that class. For example, Integer.parseInt(String str) or String.copyValueOf(char data).
Hopefully this is semi-coherant and helpful.
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
More thoughts: The reason the Math class methods are all static is that you don't need any information from an instance. You pass a number to Math class, it calculates something, and gives you an answer. There is no information inside any Math objects which could affect the answer you are given (in fact, Math class has been engineered so there never can be any Math objects).
So, if you are just passing information and want a reply on that information alone, rather than using information from the instance, you can use a static method. You can use static methods to get an object of the class when using factory methods, or deSerialize() methods. In the case of deSerialize, you don't have an object to start with, so you tell the method where to find the serialized version, the deSerialize method re-creates the object, and passes it back. After which it becomes an ordinary object which you can use as normal. CR
For us beginners, the advantage is you can call a method without having to create a variable of that class.
Like Math.pow(3.0,2.0) or Math.random()
Joined: Jul 13, 2006
Thanks for all the information which you have provided. Can you also clear my one more doubt regarding the usage of static methods!
I am writing a Bean class, which is declared public. I have some methods in it which have return type as ArrayList. I have initialized separate ArrayList object's for usage in each and every method.
I am creating instance of this class in a JSP page and getting the ArrayList values from the Bean class methods. Now what I am not clear is, in this situation can I make those methods static and can I create only one ArrayList object which is declared as static for using in each of the methods of the Bean class?
As per the information provided in these posts, what I could understand is whenever we don't have further usage of a method/variable we can declare it as static and use it wherever required.
Am I able to understand the concept well? Please clarify....
For clarification on my question I am pasting a part of code from my Bean class:
and some more methods like this...
[ July 21, 2006: Message edited by: Sree Mami ] [ July 21, 2006: Message edited by: Sree Mami ]