Static Method in Java

Let’s see, What is static Method in Java. You can use the static keyword before method name to make the method static. Some facts about static methods-

  • Static Method belongs to the class and not to the object(Instance of the class).
  • The static method can access only static data. It can’t access non-static data of the class.
  • The static method can call only other static methods and can’t call a non-static method from it.
  • Static method can be accessed directly by the class name and doesn’t need any object (using class name.MethodName)
  • A static method cannot refer to “this” or “super” keywords in any way.

 

Output:

Hi, I am from Static Method and my company is EasyBix

Hello, I am from Non-Static Method
My Name is Merrilla and my company is EasyBix

 

Video Tutorial in Automation Testing:

 

Execution Order of Static Methods:

  • Static methods are executed in the order of they are called, not in the order of they are defined in the class.
  • All static methods are executed in java stack area by creating separate stack frame (Will discuss this later).
  • When a method is called from main() method, JVM creates stack frame in main() thread for that method execution.
  • The stack frame is destroyed immediately after method execution is completed.

 

0 Comment

Leave a Reply