Solved

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

Posted on 2001-09-15
5
817 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 106
eclipse shortcuts 9 55
factorial example 4 40
Running JavaFX on JDeveloper 12C 1 32
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

810 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