< Prev
Next >



Java static keyword





The main feature of a static keyword is when anything marked with a static keyword is initialized with a value, this value remains the same value for all the objects of a class.




In Java, static keyword is used to create a -







A static variable/static method declared within a class, can be accessed -



class A
{

static int value=10;	            //static variable of class A

public void setValue(int v)
{
value=v;
}

public int getValue()
{
return value;
}


public static void main(String... ar)
{
A ob1= new A();
ob1.setValue(20); 		    //Setting the value of static variable using ob1

A ob2= new A();
ob2.setValue(30);		    //Setting the value of static variable using ob2

System.out.println(ob1.getValue()); //Accessing value of static using ob1
System.out.println(ob2.getValue()); //Accessing value of static using ob2
}

}


Output is :


30
30


Program Analysis






Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< final keyword
this keyword >

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page

Notifications



COMING SOON
-
PYTHON