0

A static method belongs to the class rather than object of a class. A static method can be invoked without the need for creating an instance of a class. What does it mean?

Codor
  • 17,447
  • 9
  • 29
  • 56
Prasoon Pandey
  • 99
  • 2
  • 4
  • 13
  • It means exactly what it says. A static method is not linked to a particular instance, therefore you do not need to create an instance with `MyObject myObject = new MyObject();` to call `myObject.myMethod();`, you can just call it on the class itself without any created instance as `MyObject.myMethod();` (yes, the lower and uppercase matters) – EpicPandaForce Mar 05 '15 at 14:56
  • possible duplicate http://stackoverflow.com/questions/4581482/static-and-non-static-methods-in-java – Drew Kennedy Mar 05 '15 at 14:56
  • possible duplicate of [When should a method be static?](http://stackoverflow.com/questions/48755/when-should-a-method-be-static) – John Bupit Mar 05 '15 at 14:57
  • See [Understanding Class Members](http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) in Oracle's Java Tutorials. – Jesper Mar 05 '15 at 15:00

7 Answers7

2
ClassObject classObj = new ClassObject();
classObj.doSomething();

vs.

ExampleClass.staticMethod();

First one needs an instance of ClassObject to call doSomething(). The second one doesn't.

Ben Win
  • 830
  • 8
  • 21
2

It means that, rather than needing to create a new instance from the class and then calling the function like:

Foo f = new Foo();
f.bar();

you can instead access it directly from the class like:

Foo.bar();

That obviously means that you can't use any non-static field or method of the object from a static method, of course.

Silverlord
  • 88
  • 7
1

Here is an example of a class with a static method and standard method.

public class MyClass {
    public static void staticPrintMe() {
        System.out.println("I can be printed without any instantiation of MyClass class");
    }

    public void nonStaticPrintMe() {
        System.out.println("I can be printed only from an object of type MyClass");
    }
}

And here is the code to call both methods:

MyClass.staticPrintMe();                  // Called without instantiating MyClass

MyClass myClassInstance = new MyClass();  // MyClass instantiation 
myClass.nonStaticPrintMe();               // Called on object of type MyClass

As you can see the static method is invoked without any object of type MyClass.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0

Take the java.lang.Math class as an example. In this line of code:

double pi = 2 * Math.asin(1);

I've referred to the Math class, but the asin method is static. There's no instance of the class created, the class just acts as a placeholder for this utility function.

A corollary of this is that a static method may not access any per-instance data - it can only access class variables that are also declared static.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

Look at this example. Defining a class with both an static method and an instance method:

public class MyClass {
    public MyClass() {
        // do something
    }

    public static staticMethod() {
        // do something
    }

    public void instanceMethod() {
        // do something
    }
}

Usage:

MyClass anInstance = new MyClass();

// static method:
MyClass.staticMethod();

// instance method:
anInstance.instanceMethod();

// this is possible thought discoraged:
anInstance.staticMethod();

// this is forbidden:
MyClass.instanceMethod();
Paco Abato
  • 3,920
  • 4
  • 31
  • 54
0

To invoke static method you do not need to build a class instance (i.e object).

For example:

class Multiplier {
  public static double multiply(double arg1, double arg2) {
          return arg1 * arg2;
  }
}

static method does not use class instance information, and you can use the method as:

Multiplier.multiply(x, y);

non-static method uses class instance information and depends on it.

for example:

class Pony {
  private Color color;
  private String name;

  public Pony(Color color, String Name) {
    this.color = color;
    this.name = name;
  }

  public void printPonyInfo() {
    System.out.println("Name: " + this.name);
    System.out.println("Color: " + this.color);
  }
}


   Pony pony1 = new Pony(Color.PINK, "Sugar");
   Pony pony2 = new Pony(Color.BLACK, "Jupiter");

and when you call:

pony1.printPonyInfo();
pony2.printPonyInfo();

You get name and color for every pony object. You cannot call Pony.printPonyInfo() because printPonyInfo() does not know, what to print. It is not static. Only when you have created Pony class instance, you can call this method, that depends on the class instance information.

0

When we call a method, we need an instace of a class like this.

class SomeClass {
    public void test() {}
}

SomeClass obj = new SomeClass();
obj.test();

'obj' is an instance of SomeClass. Without an instance like 'obj', we can't call the test method.

But the static method is different.

class SomeClass2 {
    public static void testStatic() {}
}

We don't call the testStatic method like above case of SomeClass.

SomeClass2 obj = new SomeClass2();
obj.testStatic(); // wrong

We just call with only class type.

SomeClass2.testStatic();
Hanael
  • 1