-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatic.java
More file actions
32 lines (24 loc) · 1017 Bytes
/
Static.java
File metadata and controls
32 lines (24 loc) · 1017 Bytes
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
//static -> class specific; not object
//changes for all (inside the class)
class Student {
String name;
int rollNo;
static String varsityName = "UIU";
public Student(String name, int rollNo) {
this.name = name;
this.rollNo = rollNo;
}
}
public class Static {
public static void main(String[] args)
{
Student student1 = new Student("AR", 7);
Student student2 = new Student("CR", 7);
System.out.println(Student.varsityName); // "student1.varsity" will word as well
System.out.println(Student.varsityName); //UIU
student1.varsityName = "AIUB"; //changing static value will apply for all property inside the class (Student.varsityName = "AIUB"; will work too)
System.out.println(student1.varsityName); //AIUB ; "Student.varsityName" will work
System.out.println(student2.varsityName); //AIUB
// it's recommended to use class name instead of reference name while accessing the static attribute
}
}