Constructor with Args
Learning Outcomes
- Can pass an argument to a constructor
- Create a new class called RockWithArgs
| RockWithArgs |
|---|
| package com.ait.wk5;
public class RockWithArgs {
public RockWithArgs(int i) {
System.out.println("Creating Rock number " + i);
}
}
|
- Add a new class called ConstructorWithArgs
| ConstructorWithArgs |
|---|
| package com.ait.wk5;
public class ConstructorWithArgs {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
RockWithArgs rock = new RockWithArgs(i);
}
}
}
|
Creating Rock number 1
Creating Rock number 2
Creating Rock number 3
Creating Rock number 4
Creating Rock number 5
RockWithArgs.java
ConstructorWithArgs.java