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;
		ClassA.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
		obj.localCounter+=10;
		obj.counter+=10;
		
		// 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();
                ClassA.staticMethod();
		obj.staticMethod();

		
		// Accessing non-static method using Object
		obj.nonStaticMethod();
		
		// 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.");
		
	}
}

1998total visits,2visits today

Leave a Reply

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