-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcwh_41_Constructors.java
48 lines (40 loc) · 1.01 KB
/
cwh_41_Constructors.java
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
46
47
48
package company;
import java.lang.String;
class Employee3
{
private int id;
private String name;
public Employee3(int i, String n)
{
id = i;
name = n;
}
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_41_Constructors
{
public static void main(String[] args)
{
// Costructors :- A member function used to initialize an object while creating it.
// In order to write our own constructor, we need to define a method with the name same as class-name.
Employee3 em = new Employee3(4, "LordSnow");
em.setId(3);
System.out.println(em.getId());
System.out.println(em.getName());
}
}