0 votes
13 views
What is the static variable?
in Education by (48.2k points)

1 Answer

0 votes
What is the static variable?

The static variable is used to refer to the common property of all objects (that is not unique for each object), e.g., The company name of employees, college name of students, etc. Static variable gets memory only once in the class area at the time of class loading. Using a static variable makes your program more memory efficient (it saves memory). Static variable belongs to the class rather than the object.

//Program of static variable  

  

class Student8{  

   int rollno;  

   String name;  

   static String college ="ITS";  

     

   Student8(int r,String n){  

   rollno = r;  

   name = n;  

   }  

 void display (){System.out.println(rollno+" "+name+" "+college);}  

  

 public static void main(String args[]){  

 Student8 s1 = new Student8(111,"Karan");  

 Student8 s2 = new Student8(222,"Aryan");  

   

 s1.display();  

 s2.display();  

 }  

}  

Test it Now

Output:111 Karan ITS

       222 Aryan ITS

Static Variable
by (48.2k points)

Related questions

0 votes
1 answer 18 views
18 views asked Jan 17 in Technology by JackTerrance (186k points)
+1 vote
1 answer 9 views
0 votes
1 answer 13 views
13 views asked Nov 19, 2020 in Education by Editorial Staff (48.2k points)
0 votes
1 answer 10 views
0 votes
1 answer 12 views
0 votes
1 answer 14 views
0 votes
1 answer 20 views
20 views asked Nov 6, 2020 in Technology by JackTerrance (186k points)
0 votes
1 answer 15 views
0 votes
1 answer 12 views
12 views asked Nov 19, 2020 in Education by Editorial Staff (48.2k points)
0 votes
1 answer 10 views
0 votes
1 answer 11 views
0 votes
1 answer 11 views
0 votes
1 answer 22 views
0 votes
1 answer 25 views
+1 vote
1 answer 11 views
...