static variable with constructor and method

Java Programming Language / Class, Object and Methods in java

1210

Program:

 //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).
*/

  

Output:

1 Rahim Java Programming Language
2 Ram Java Programming Language
Press any key to continue . . .

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.