I am developing application..
class Wheel {
private int size;
Wheel(int s) {
size = s;
}
void spin() {
System.out.print(size + " inch wheel spinning, ");
}
}
public class Bicycle {
public static void main(String[] args) {
Wheel[] wa = { new Wheel(15), new Wheel(17) };
for (Wheel w : wa)
w.spin();
}
}
But Please advise that how could we express Wheel[] wa = { new Wheel(15), new Wheel(17) }; in more simpler terms.
If you want a shorter expression for creating an array of wheels, what you have is a simple as it can get without additional methods.
You could create a factory method like so:
Which would make your wheel creation statement:
Which isn’t much simpler when you only have two wheels of predetermined size, but if you have more of them, or if the sizes aren’t predetermined, this might be more readable.