Home / JAVA / Static keyword in java

Static keyword in java

Static keyword is often used in java for various purpose. Different programmers use different keyword according to their convenience and requirement However one could not move away from the fact that static keyword is frequently used in Java.projects

We can use “static” in java in three ways:

1. static method
2. static variables
3. static block

Static Method

In this we make the method of a class static which means that static method defined in class will remain same for all class.Also we can say that static methods are common for all objects. Static methods can be call through the class name ,also it can be called directly without any class name as well as object name before it.But for this case static method should be called in the same class where the static method has been defined. If we call the static method in different class than we can call it from the class name before it. Also static methods can be called from the objects in the similar manner as we call the instance methods. But static methods are common for all the objects, where as instance methods are different for all objects. Example of static function is the public static void main function of java which is static refers that this main function remains same for all objects.

class Static_class
void Function1()
System.out.println("Hello this is simple method");
static void Function2()    // static method
System.out.println("Staic method is common for all objects");

public static void main(String args[])
Static_class obj=new Static_class();    // creation  of object
obj.Function1();  // calling instance method
obj.Function2();  // calling static method
Function2();  // calling static method when calling from the same class
Static_class.Function2();  // calling static method, can be called from different class
}  // end of  class


Also See:   Java program for binary to decimal conversion

static method in java

Static Variable

Static variables are common for all the objects and they have the default values defined in java .Also we can say that value of the static variables did not change while calling from different objects. But using the instance function which are different for the different objects we called in the class.

We can use instance variables and static variables in the instance methods. But we cannot use instance variables in static methods, for this we use objects.

class Static_variable
int x;   //  instance variable
static int a;  // static variable
public static void main(String args[])
Static_variable obj=new Static_variable();  // creation of 1st object
Static_variable obj1=new Static_variable();  // creation of 2nd object
obj.x=20;    // assigning the value to instance variable
System.out.println("Value of x is "+obj.x);   

Static_variable.a=20;  // assigning the value to static variable
System.out.println("Value of a is "+a);  
System.out.println("Value of a for 1st  object is "+obj.a);
System.out.println("Value of a for 2nd object is "+obj1.a);
}    //  end of class


Also See:   How to use final keyword in Java

static variable in java


Static Block

Coming Soon …




Check Also

Java program to implement polymorphism

Java program to implement polymorphism: Polymorphism is a technique in java with which you can ...

Leave a Reply

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