Advertisement



< Prev
Next >



Java static keyword





The main feature of a static keyword is that when anything marked with a static keyword in a class, it belongs to the class and not to the class's object.




In Java, static keyword is used to create :







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 -




< 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

PYTHON and DJANGO


Advertisement