-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTut16.java
More file actions
36 lines (28 loc) · 713 Bytes
/
Tut16.java
File metadata and controls
36 lines (28 loc) · 713 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
33
34
35
36
package tutorial;
// access modifier with getter and setter
class Student{
private int id;
private String name; //private means only class can access it
// for more access modifier see the wiki
public int getid() {
return id;
}
public String getname() {
return name;
}
public void setid(int id) {
this.id=id;
}
public void setname(String name) {
this.name=name;
}
}
public class Tut16 {
public static void main(String[] args) {
Student st=new Student();
//note now you cannot access the student class attribute directly you need to use the getter and setter
st.setid(123);
st.setname("Prahtamesh");
System.out.println("ID : "+st.getid()+"\nName : "+st.getname());
}
}