//Program of static variable
class StudentClass{
int rollno;
String name;
//static varible with initialization
static String subject ="Java Programming Language";
StudentClass(int r,String n){
rollno = r;
name = n;
}
void show (){
System.out.println(rollno+" "+name+" "+subject);
}
public static void main(String args[]){
StudentClass obj1 = new StudentClass(1,"Rahim");
StudentClass obj2 = new StudentClass(2,"Ram");
obj1.show();
obj2.show();
}
}
/*
No need to create object in same class
If you declare any variable as static, it is known static variable.
The static variable gets memory only once in class area at the time of class loading.
It makes your program memory efficient (i.e it saves memory).
*/
1 Rahim Java Programming Language
2 Ram Java Programming Language
Press any key to continue . . .
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.