Can static variables be called using className.variableName in Java?


Class variables also known as static variables are declared with the static keyword There would only be one copy of each class variable per class, regardless of how many objects are created from it. You can access a class variable without instantiation using the class name as className.variableName.

Example

Live Demo

public class Test{
   static int num = 92;
   
   public static void main(String args[]) throws Exception {
      System.out.println(Test.num);
   }
}

Output

92

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements