-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructors.java
More file actions
31 lines (29 loc) · 1.09 KB
/
constructors.java
File metadata and controls
31 lines (29 loc) · 1.09 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
class MyMainEmployee{
private int id;
private String name;
public MyMainEmployee(){ //this is a constructor, it is a special method that is called when an object of the class is created. It is used to initialize the object. It has the same name as the class and does not have a return type.
id = 4;
name = "vanshi";
}
public void setname(String n){
name =n;
}
public String getname(){
return name;
}
public void setid(int i){
id = i;
}
public int getid(){
return id;
}
}
public class constructors {
public static void main(String[] args) {
MyMainEmployee vanshi = new MyMainEmployee();
//vanshi.setid(56); //using the setter method to set the value of id
//vanshi.setname("Vanshi"); //using the setter method to set the value of name
System.out.println(vanshi.getid()); //using the getter method to get the value of id
System.out.println(vanshi.getname()); //using the getter method to get the value of name
}
}