Solved

When and when not to declare a method static ..?

Posted on 2001-09-15
5
815 Views
Last Modified: 2012-08-13
Is there any detailed article about the mentioned question or any expert can guide about static declation of method.
0
Comment
Question by:agsingh
5 Comments
 
LVL 1

Expert Comment

by:vbremaud
Comment Utility
You have 2 kinds of methods:
 -methods that belong to the object
 -methods that belong to the class

Methods that belong to the object can access other methods are fields belonging to the object, as well as those belonging to the class.
Methods that belong to the class can only access other methods or fields belonging to the class, as there is no instance of the class (that is the object).

Methods and fileds belonging to the class are those declared static.

hope it helps :-)
0
 
LVL 6

Expert Comment

by:jpk041897
Comment Utility
You would want to declare a method as static only if you want to be able to ibvoke that method before an instance of the class has been created.

For instance:

class MyClass{

   int someAttribute;

public static void main(String args){
   MyCLass c = new MyClass();
...


In this case, main is declared as static so that it can be invoked (from the command line) before its instantiated.

Its important to note that a methos invoked in this fashion does not have access to the classes attributes since they have not been instantiated yet.


Another example would be the use of Class.forName() in JDBC.

In general, you want to declare a method static when you want to be able to invoke it without the overhead involved in instantiating the full class. On the other hand, methods invoked in this fashion are sevearly limmited in what they can do since they cannot access their own class level attributes or other methods in the same class unless these members are also declared static.

Generaly speaking, you would want to use static members either for simple operations, inner class method invocation singleton class pattern invocation or for some cases of class factory pattern implementation.

In all other cases you should probably avoid it.


Hope this helps.
0
 
LVL 4

Expert Comment

by:k.jones
Comment Utility
See a good discussion at following url.

http://www.jguru.com/faq/view.jsp?EID=254995

Cheers,
Ken Jones
0
 

Author Comment

by:agsingh
Comment Utility
i am getting some idea about it ....thanx.
but clearify this also...

my method is just taking some arguments as parameters and based on these arguments only its returning some value. If i declare this method as static , what is the advantage/disadvantage of it.(i am not using any class variable)
What happens if this method is being used by many ...here what i mean is, like static variables are being shared, what happens when we use a static method.

thanx
0
 

Accepted Solution

by:
hajava earned 50 total points
Comment Utility
It's a question of design. The answer lies in the purpose of your class.

If you have an utility method like java.lang.Math.max(), it's a good idea to declare that method static. This way, you don't have to instatiate an object every time just for using that method (you don't need that object anyway).

But: A class models an abstraction by defining the properties and behaviours for the objects represented by the abstraction.

That means: If your method is a behaviour for the object (like Bill.pay() or Car.stop()) it would *not* be a candidate for being static, because it has nothing to do with the class, but with the object. OTOH if your method is not a behaviour of certain object and has to do with the class as such, or with all objects of that class (like Bill.howManyBillsNotPaid() or Car.howManyFuelInAllCars()) then it should be static.

Heinz
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now