-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
40 lines (32 loc) · 1.08 KB
/
Copy pathApp.java
File metadata and controls
40 lines (32 loc) · 1.08 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
32
33
34
35
36
37
38
39
40
package tutorial13;
class Machine{
// couple private class variables not accessible from outside of class
private int code;
private String name;
// class constructor.. no return type, and name must match class name.. method runs each time you instantiate instance
public Machine(){
// can run other constructors to add defaults for instance by using this .. must be first statement though
this("default",1);
System.out.println("contructor running");
}
// can have multiple constructors.. differing by parameters passed, compiler will pick right one
public Machine(String name){
this.name = name;
System.out.println("second constructor running");
}
public Machine(String name, int code){
this.name = name;
this.code = code;
System.out.println("third constructor running");
}
}
public class App {
public static void main(String[] args) {
// just making an instance of Machine, runs the class constructor
Machine roby = new Machine();
// don't have to name the object to instantiate it
new Machine();
new Machine("tyrone");
new Machine("betty", 101);
}
}