0% found this document useful (0 votes)
4 views1 page

Java Static Method Example in Student Class

The document defines a Java class 'Student' with a static variable 'college' and a static method 'change' to modify it. The 'Student' class constructor initializes student details, and the 'display' method prints them. The 'TestStaticMethod' class demonstrates the usage of the 'Student' class by creating instances and displaying their information after changing the college name.

Uploaded by

emeetabhi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Java Static Method Example in Student Class

The document defines a Java class 'Student' with a static variable 'college' and a static method 'change' to modify it. The 'Student' class constructor initializes student details, and the 'display' method prints them. The 'TestStaticMethod' class demonstrates the usage of the 'Student' class by creating instances and displaying their information after changing the college name.

Uploaded by

emeetabhi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

.

class Student{
int rollno;
String name;
static String college = "EWIT";
//static method to change the value of static variable
static void change(){
college = "SJBIT";
}
//constructor to initialize the variable
Student(int r, String n){
rollno = r;
name = n;
}
//method to display values
void display(){[Link](rollno+" "+name+" "+college);}
}
//Test class to create and display the values of object
public class TestStaticMethod{
public static void main(String args[]){
[Link]();//calling change method
//creating objects
Student s1 = new Student(111,"Kiran");
Student s2 = new Student(222,"Aryan");
Student s3 = new Student(333,"Sonu");
//calling display method
[Link]();
[Link]();
[Link]();
}
}

You might also like