File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Using static keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using static keyword" Watch "Using static keyword" New topic

Using static keyword

Sree Mami
Ranch Hand

Joined: Jul 13, 2006
Posts: 75
Hi all

Can anyone explain me what is the difference between declaring a method as static and not declaring it as static in a class; likewise with a variable.
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
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
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
Well, I thought you were coherent, Rusty.

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.
Larry Eisenstein

Joined: Feb 21, 2006
Posts: 14
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()
Sree Mami
Ranch Hand

Joined: Jul 13, 2006
Posts: 75
Hi all

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 ]
javablogger owiusa

Joined: Jul 22, 2006
Posts: 4
if u want to maintain an information common to ur class, then mark that information as static ,

note : information may be static method or static variable

why it is common information?

Ans: becoz it is not specific to object, ie object won't have its own copy of static variable , instead all objects will point to the static information,thats why it is common information

so any object change that information ,will reflect to all other object
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

"javablogger owiusa",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

JavaRanch Sheriff

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Using static keyword
It's not a secret anymore!