Koray Tugay Koray Tugay - 1 year ago 121
Java Question

What is the default value for an Enum type instance variable in Java?

This is the sample code I have:

enum A {

class TestA {
A a;
public static void main(String[] args) {
final TestA testA = new TestA();

Which will print:


If the default value for an uninitialized instance Enum variable is null, how does accessing an instance of an Enum work?

Answer Source

A.A is a static variable. It's a bad idea, but authorized, to access static variable of a class using a variable referring to an instance of that class, even if it's null. That's not limited to enums:

Integer i = null;

runs fine. But it should really be written as

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download