What is the static variable?

DWQA QuestionsCategory: JavaWhat is the static variable?
Editor Staff asked 10 months ago

What is the static variable?

1 Answers
Editor Staff answered 10 months ago

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”);

Test it Now
Output:111 Karan ITS
222 Aryan ITS
Static Variable