1

How do static methods actually run in Java? We usually invoke them using the class name, and do not create any object for them, but these are objects which actually can "run", not methods!

Greenonline
  • 1,330
  • 8
  • 23
  • 31
John
  • 201
  • 1
  • 7

2 Answers2

4

For static methods, a special keyword is available and added as part of the byte-code. It is called invokestatic.

From Oracle docs :

invokestatic

Operation

Invoke a class (static) method

Description :

The unsigned indexbyte1 and indexbyte2 are used to construct an index into the run-time constant pool of the current class (§2.6), where the value of the index is (indexbyte1 << 8) | indexbyte2. The run-time constant pool item at that index must be a symbolic reference to a method (§5.1), which gives the name and descriptor (§4.3.3) of the method as well as a symbolic reference to the class in which the method is to be found. The named method is resolved (§5.4.3.3). The resolved method must not be an instance initialization method (§2.9) or the class or interface initialization method (§2.9). It must be static, and therefore cannot be abstract.

On successful resolution of the method, the class that declared the resolved method is initialized (§5.5) if that class has not already been initialized.

The operand stack must contain nargs argument values, where the number, type, and order of the values must be consistent with the descriptor of the resolved method.

Sample code :

public class Example {

static void myStaticMethod() {
    System.out.println("Hello");
}

public static void main(String[] args) {
    myStaticMethod();
}

}

Byte code :

public static void main(java.lang.String[]);
   descriptor: ([Ljava/lang/String;)V
   flags: ACC_PUBLIC, ACC_STATIC
   Code:
     stack=0, locals=1, args_size=1
        0: invokestatic  #31                 // Method myStaticMethod:()V
        3: return
     LineNumberTable:
       line 8: 0
       line 9: 3
     LocalVariableTable:
       Start  Length  Slot  Name   Signature
           0       4     0  args   [Ljava/lang/String;

Runtime constant pool of class :

...
 #31 = Methodref          #1.#32         // Example.myStaticMethod:()V
 ...
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

In case of static method Java loads the method when the class is loaded and shares the method with all the objects also and that's why an object is not necessary.
It's not always necessary to have an object in order to run a method.

And no, it's not the object that runs. It's the method that does the operation and objects or classes hold them as a property.

Greenonline
  • 1,330
  • 8
  • 23
  • 31
Saif
  • 6,804
  • 8
  • 40
  • 61
  • Note : Even *non-static* methdos are *loaded* when the class is loaded and there will only be *one instance* of non-static methods as well. – TheLostMind May 14 '15 at 08:58
  • @TheLostMind thanks for your information, I really didn't know about that. Can you please provide me some link regarding this i mean loading of non static method. Thanks again – Saif May 14 '15 at 09:26
  • [this](http://stackoverflow.com/questions/11821974/jvm-hotspot-where-do-all-the-methods-go-method-area-native-method-stack) might help. You might also want to go through JVM spec :) – TheLostMind May 14 '15 at 09:30