Static Method and Static Variable in Java

Static Method and Static Variable in Java


Static Method and Static Variable: In Java, static is keyword that can be applied to variables and methods but not to classes. static is used as an access modifier.

In Java, to call an instance variable or method, an object is required. But a static variable and method can be called without the help of an object. This is the basic difference between static and non-static in Java.

Observe the following code on Static Method and Static Variable.


Static Method

Observe, average and show() are static members of the class Demo and are called without the need of an object. marks and display() are non-static members and are called with object d1.

Note: More indepth explanation is available at Java static Variable Method.

In and Outs on static Usage. Read when you are comfortable with the above example

  1. Can a local variable be static?

Leave a Reply

Your email address will not be published. Required fields are marked *