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 What is the use of STATIC variable, calss, method?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the use of STATIC variable, calss, method??" Watch "What is the use of STATIC variable, calss, method??" New topic

What is the use of STATIC variable, calss, method??

rakesh lawr

Joined: Sep 17, 2009
Posts: 1
Hi friends
I need a help from you all.
I want to know about the usage of STATIC keyword in a class/method/variable...

Memory allocation is one advantage for STATIC keyword...
any more advateags with STATIC???

Please help me
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Static variables for instance are also called 'class variables'. They only exist once per class, not per instance.
That makes static members instance independent, they share the same value among all instances of your class.

A good example for static methods are utility methods such as those in the Arrays or Collections class.
To perform Collections.emptyList() you do not need an instance because this method does not require any state. Hence it is a good decision to mark it static.

JDBCSupport - An easy to use, light-weight JDBC framework -
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
Welcome to JavaRanch

Agree with Sebastian Janisch. About methods: if a method takes no information from the state of an object (it uses no instance fields) and it imparts no information to the state of an object (it alters or manipulates no instance fields) then you should consider making it static

Note that many books use the main method a lot, because it is quicker to put a little code example in the main method than to write proper object-oriented code. So they have examples where all their methods are marked "static" because you cannot access instance fields, instance members, the keyword "this" and the keyword "super" in a static method or block.
sean cotter

Joined: Sep 16, 2009
Posts: 4
When you have an object that you only want 1 instance of, you use static.
A very good example of this is a database connection pool. You may want many connections but since creating connections is very expensive operation, so you want to reuse them. That is where the database pool comes in (a pool of database connections).
your database is configured for 110 connection and your database pool is configure for 100 connections. If you have 2 instances of the database pool, you can imagine the problems that can occur when one of those pools ask the database for its 111th connection
There is a very popular design pattern based on the static keyword it is called a singleton. Here is one article.

Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
Also: I would not think in terms of the advantages of one versus the other. Instead, they each have a distinct purpose.
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

rakesh lawr wrote:I want to know about the usage of STATIC keyword in a class/method/variable...

Campbell and Sebastian has already defined why the method should be static. I would only suggest to
remember when you make method static is that you must make sure there
is no chance you’ll want it to behave polymorphically.
I agree. Here's the link:
subject: What is the use of STATIC variable, calss, method??
It's not a secret anymore!