Advertisement



< Prev
Next >



Java static keyword





The main feature of using the static keyword is that, when anything is marked with the static keyword in a class, it belongs only to the class and not to the objects of the class. Now let us see, what in a class can be marked by using the static keyword.




In Java, static keyword is used to create :







A static variable/static method of a class can be accessed in two places-



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 -




< Prev
Next >
< final keyword
this keyword >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement