Solved

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

Posted on 2001-09-15
5
816 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
ID: 6484910
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
ID: 6484939
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
ID: 6485173
See a good discussion at following url.

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

Cheers,
Ken Jones
0
 

Author Comment

by:agsingh
ID: 6485485
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
ID: 6486089
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

920 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

16 Experts available now in Live!

Get 1:1 Help Now