Ace Java 2025: Rock Your 'Thinking in Java' Challenge!

Question: 1 / 510

How can you get enum instances if 'values()' is not available due to upcasting?

Using 'getEnumConstants()' method

After upcasting, the enum class is seen as a normal class, therefore the 'values()' method is no longer available. An alternative method to get the enum instances is by using the 'getEnumConstants()' method, which will return an array of all the enum instances. Option B, using reflection, is incorrect because it is not efficient and can only obtain the enum values one by one. Option C is also incorrect because, as mentioned before, the 'getEnumConstants()' method can return the enum instances. Option D, using 'valueOf()' method, is incorrect because this method only takes in a string parameter and returns an enum constant, not all the enum instances. Therefore, the correct answer is A.

Get further explanation with Examzify DeepDiveBeta

With reflection

Cannot get instances

Using 'valueOf()' method

Next Question

Report this question

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy