Basic Java – 7 || Static Variables and Methods.

        The synonyms of static, is unchangeable or fixed, means value doesn’t get change from external behaviors. In Java, static variables and methods can be called directly using the class name without creating any object.
Static Variables
Static Variables
           In other words, the static keyword used to define a method or variable which will be used Globally over the same class and multiple classes. There will be only one copy of the static variable throughout the program in the memory for all the objects created during the program execution.
        On the other hand, non-static variable reserves separate space in the memory with each new Object creation.
Related Links:

OOPs Concept:

Java Question And Answer:

 Key Points:
  1. Static variables can only be declared at Class level.
  2. Static variables cannot be declared inside any method.
  3. To make a static method or variable use static keyword.
  4. Static variable/method can be accessed using the Class name without any object as well as using object of that class.
  5. Cannot call a non-static method/variable using the Class name without any object.
  6. Static method/variable can also be called directly using a method or variable name in the same class but in a static method only.

       The best example of a static method is main() , as we know that main()  method get called automatically whenever we execute any JAVA Program without creating any object.

To get more information please visit:


Code Example

public class ClassA 
	// Declaring static variable
	static int counter=10;
	// Declaring non-static variable
	int localCounter=10;

	public static void main(String[] args)
		System.out.println("Accessing static variable without any Object: "+ClassA.counter);
		// Adding values to static variable with or without using Class name.
                // counter+=10;
		// To access non-static variable we have to create an object of Class
		ClassA obj=new ClassA();
		System.out.println("Accessing non-static variable using First Object: "+obj.localCounter);
		System.out.println("Accessing static variable using First object: "+obj.counter);
		//Adding values to non static variable
		// Creating another Object of Class
		ClassA newObj=new ClassA();
		System.out.println("Accessing non-static variable using Second Object: "+newObj.localCounter);
		System.out.println("Accessing static variable using Second object: "+newObj.counter);
		// The same way we can access static methods too.
                // staticMethod();

		// Accessing non-static method using Object
		// Following statements throws Compile Time error "remove static modifier"
		// static int counter=0;
		// ClassA.nonStaticMethod();
	public void nonStaticMethod()
		System.out.println("Calling a non-static method.");
	public static void staticMethod()
		System.out.println("Calling a Static method.");

Leave a Comment

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

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.

Facts about main() Method
  1. A main() method is an entry point for a java program.
  2. main() would be called by the JVM using the class directly without creating an object. That’s why we declare the main method ‘public’ and static.
  3. main() would never return any value, so we kept return type void.
  4. main() would take parameters in the form of String Array. Which would be used to passing input values at runtime.
To get more information please visit:
Basic Java – 3 || Understanding Class, Objects, Methods in Java.