Simple Constructor
Learning Outcomes
- File name is the same as the public class (not Rock.java)
- Constructor has :
- same name as class name
- no return type at all (not even void)
- Constructor is called when object instantiated/created i.e. when "new" executed
- Methods which happen to have the same name as the constructor
are not called when an object of the class is instantiated
- How to call an ordinary (non-static) method in the class
- Modify the class Rock as shown to add a constructor and a method that has the same name as the constructor.
| Rock.java |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13 | package com.ait.wk5;
public class Rock {
public Rock() {
System.out.println("Creating Rock...");
}
public int Rock() {
System.out.println("Just a method");
return 9;
}
}
|
- Create the class SimpleConstructor. Run it and observe the output.
| SimpleConstructor.java |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | package com.ait.wk5;
public class SimpleConstructor {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Creating instance " + i + " of Rock");
Rock rock = new Rock();
}
Rock rock = new Rock();
rock.Rock(); // method names should start with lowercase
}
}
|
Creating instance 1 of Rock
Creating Rock...
Creating instance 2 of Rock
Creating Rock...
Creating instance 3 of Rock
Creating Rock...
Creating instance 4 of Rock
Creating Rock...
Creating instance 5 of Rock
Creating Rock...
Creating Rock...
Just a method
Rock.java
SimpleConstructor.java