-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConst2.java
More file actions
44 lines (36 loc) · 950 Bytes
/
Copy pathConst2.java
File metadata and controls
44 lines (36 loc) · 950 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
44
//This is practice in using constructors
public class Const3{
private String name;
private int age;
Const3(int y){
this.setAge(y);
}
Const3(String x){
this.setName(x);
//this.setName("David");
//System.out.println("Hello " + x);
}
public void setAge(int y){
this.age = y;
}
public void setName(String x){
this.name = x;
}
public int getAge(){
return age;
}
public String getName(){
return name;
}
}
//}
public class Const2{
public static void main(String[] args){
//notice that once I create an object, the constructor called automatically
Const3 b = new Const3("David");
System.out.println(b.getName());
Const3 a = new Const3(16);
System.out.println(a.getAge());
System.out.println(b.getName() + " is " + a.getAge() + " years old.");
}
}