Solved

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running JavaFX on JDeveloper 12C 1 112
jmss example java 2 48
how to debug htl and js pages 8 78
jsp CRUD operations with and without prepared statement also hibernatge 1 50
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

734 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