I wish to create an array of all the Colors predefined in java.awt.Color in order to randomly select one of them.
My current best attempt is:
` Color[] colors = Color.getClass().getEnumConstants();
which was suggested in the top answer to the question: Color Class in Java
but that generates the error:
Cannot make a static reference to the non-static method getClass() from the type Object
The constructor in which the erroneous call is made is below:
private Ball() {
Random initialSetter = new Random();
ballX = marginSize + initialSetter.nextInt(xSize - 2 * marginSize);
ballY = marginSize + initialSetter.nextInt(ySize - 2 * marginSize);
ballXV = initialSetter.nextInt(doubleMaxV) - doubleMaxV/2;
ballYV = initialSetter.nextInt(doubleMaxV) - doubleMaxV/2;
Color[] colors = Color.getClass().getEnumConstants();
color = colors[initialSetter.nextInt(colors.length)];
}
Replacing ".getClass().getEnumConstants()" with ".values()" generates much the same error (static reference to non-static method).