Professional Documents
Culture Documents
1) Static variable
Syntax
className.variableName=10;
className.methodName();
class Student
{
int rollno;
String name;
static String institute ="IANT";
Student(int r,String n)
{
rollno = r;
name = n;
}
void display ()
{
System.out.println(rollno+" "+name+" "+ institute);
}
s1.display();
s2.display();
}
}
class Counter
{
static int count=0;//will get memory only once and retain its value
Counter()
{
count++;
System.out.println(count);
}
obj_ref.variable_name class_name.variable_name
2) Java static method
void show()
{
System.out.println("Java is awesome.");
}
}
class Student
{
int rollno;
String name;
static String school = "SP";
Student(int r, String n)
{
rollno = r;
name = n;
}
void display ()
{
System.out.println(rollno+" "+name+" "+school);
}
public static void main(String args[])
{
Student.change();
s1.display();
s2.display();
s3.display();
}
}
Yes, one of the way is static block but in previous version of JDK not
in JDK 1.7.
class A
{
Static
{
System.out.println("static block is invoked");
System.exit(0);
}
}