Question:
Java question (static and instance methods)?
Test Test
2011-04-12 16:23:21 UTC
What action is possible with a static method that isn't possible with an instance method?
Five answers:
Jared
2011-04-12 16:37:06 UTC
"action" is a very general term. You can do anything with a static method that can be done with non-static method. If anything, you are more restricted in a static method.



You cannot "access" any member variables of the class in a static method (that is any variables NOT declared as static).



The reason that you can do anything with a static method, is that non-static method can ALWAYS be transformed into static methods by simply passing the object to the static method (it WON'T look identical, but the same "actions" can be achieved):



Example:



public class Square{



private double x;//side length of square



public double getArea(){

return x*x;

}



public void setLength(double x){

this.x = x;

}



public double getX(){

return x;



public static double getArea(Square s){

return s.x * s.x;

}



public static void setLength(Square s, double x){

s.x = x;

}



public static void getLength(Square s){

return s.x;

}

}



So you can operate on member fields in static methods AND you can even change the value of member fields (as long as they aren't declared final). Therefore I really don't know what is meant by what action cannot be done by an instance method versus static method.



Notice that for the static methods you ALWAYS have to supply the object you want it to operate on...that is what makes static methods different. Generally speaking if the object is needed by the method, then it should be made as an instance method, if not, if the method can be done with NO knowledge of the object, then it should be made static. Generally, so-called "helper" functions are a good candidate to become static methods.



Edit:



To understand what I mean by "You cannot "access" any member variables of the class in a static method", notice that in my setLength method, I use the keyword "this". "this" refers to the object that called the method. For static methods, there is NO calling object, therefore using the "this" keyword in a static method is a compile time error. If you use "this" in EVERY single instance method, you will see the exact difference in the non-static methods versus the static ones that I supplied (where you always pass the object Square s as a parameter). If you take all of those static methods and remove the keyword static and the parameter Square s, and replace instances of s with the keyword "this", then they become the non-static equivalents.
Ash
2011-04-12 16:25:40 UTC
static method can be used throughout the program, instance method can only be used within the class it was created or on an object of that class
JW
2015-04-26 18:54:05 UTC
They can be called without any instances of the class being in existence.
anonymous
2016-12-03 02:07:02 UTC
public classification A { public String getName(){ return "occasion"; } public static String getDesc(){ return "static"; } } //to apply the example technique A a = new A() a.getName(); //to apply the static technique A.getDesc();
anonymous
2014-11-16 00:51:47 UTC
extremely tough point. query onto google and yahoo. this may help!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...