-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniversity.java
More file actions
45 lines (36 loc) · 1.65 KB
/
Copy pathUniversity.java
File metadata and controls
45 lines (36 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class University {
static String universityName = "Global University"; // Static variable shared among all instances
String studentName; // Non-static variable unique to each instance
// Constructor to initialize studentName
public University(String studentName) {
this.studentName = studentName;
}
// Static method to display the university name
static void displayUniversityName() {
System.out.println("University Name: " + universityName);
}
// Non-static method to display student details
void displayStudentDetails() {
System.out.println("Student Name: " + studentName + ", University: " + universityName);
}
public static void main(String[] args) {
// Calling the static method (no object required)
University.displayUniversityName(); // ✅ Allowed
// Creating multiple student objects
University student1 = new University("Nitanshu");
University student2 = new University("Nit");
University student3 = new University("Anshu");
// Displaying student details
student1.displayStudentDetails();
student2.displayStudentDetails();
student3.displayStudentDetails();
// Modifying the static variable
University.universityName = "UPES";
// Displaying university name again to see the change
University.displayUniversityName();
// Displaying student details again to see the effect of changing the static variable
student1.displayStudentDetails();
student2.displayStudentDetails();
student3.displayStudentDetails();
}
}