Home / Programs / static variable with constructor and method
🚀 Programming Example

static variable with constructor and method

👁 1,338 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

💻 Program Code

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

  
                        

🖥 Program Output

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

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

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.

🔥 Practice suggestion

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.