Java static variable-------If we declare any variable as static it is known as static variable.It includes-----
a.The static variable gets memory only once in the class area at the time of class loading.
b.Static variables in Java can be accessed by directly classname(classname.variablename) It does not need to create an instance of the class.
Example----class student
{
int rollno; //instance variable
String nm; //instance variable
static String college="UPTU"; //static variable
void show()
{
//code
}
//code
}
//Java non-static inner class program
class outer
{
class inner
{
void show()
{
System.out.println("inner is non-static class");
}
}
}
class test1
{
public static void main(String args[])
{
outer obj=new outer();
//to call non-static class
outer.inner obj1=obj.new inner();
obj1.show();
}
}
//Java static variable program
class examp
{
int rno; //instance variable
String nm; //instance variable
static String college; //static variable
void display()
{
System.out.println("rno="+rno);
System.out.println("nm="+nm);
System.out.println("college="+college);
}
}
class student
{
public static void main(String[] args)
{
//classname.variablename
examp.college="UPTU";
//classname objectname=new classname();
examp obj=new examp();
//objectname.variablename
obj.rno=102;
obj.nm="Ram Singh";
//objectname.methodname();
obj.display();
}
}