forked from ParthMurge/Java-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcwh_40_getters_and_setters.java
More file actions
43 lines (36 loc) · 935 Bytes
/
cwh_40_getters_and_setters.java
File metadata and controls
43 lines (36 loc) · 935 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
37
38
39
40
41
42
43
package company;
import java.lang.String;
class Employee2
{
private int id;
private String name;
public void setName(String a)
{
this.name = a; // We can also write "name" as "this.name"
}
public void setId(int a)
{
id = a;
}
public String getName()
{
return name;
}
public int getId()
{
return this.id; // We can also write "id" as "this.id"
}
}
public class cwh_40_getters_and_setters
{
public static void main(String[] args)
{
Employee2 em = new Employee2();
// em.name = "HeParth";
// em.id = 19; -->This will show error bqz of the private access modifier, and so we use getters and setters.
em.setName("He_Parth");
em.setId(19);
System.out.println("\n" +em.getName());
System.out.println(em.getId()+ "\n");
}
}