Solved

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

Posted on 2001-09-15
5
820 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

763 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