(Java) Static member accessed via instance reference with enumerators



I just started learning Java. IntelliJ is giving me a warning “Static member accessed via instance reference” on line 4. Is it bad, should I fix it, somehow, or should I just ignore it?

Here is my code:

public class MainClass {
    public static void main(String[] args) {
        Dog randomDog = new Dog();
        Dog myDog = new Dog(4, "Charlie", new Dog().breed.Labrador);
        System.out.println("Random dog's name is: " + randomDog.name + ", it's age is: " + randomDog.age + " and it's breed is: " + randomDog.breed);
        System.out.println("My dog's name is: " + myDog.name + ", it's age is: " + myDog.age + " and it's breed is: " + myDog.breed);
    }
}

and the Dog class:

public class Dog {
    int age;
    String name;
    enum breed { Poodle, Shepherd, Labrador }
    breed breed;

    Dog(int age, String name, breed breed) {
        this.age = age;
        this.name = name;
        this.breed = breed;
    }
    Dog() {
        this.age = 0;
        this.name = "Rex";
        this.breed = breed.Labrador;
    }
}

Answer

One issue (which causes others) is that you’re hiding the type breed by also having a field of the same name in the same scope.

That’s a very rare problem to have, because the naming conventions of Java usually prevent this kind of clash: Types (classes, interfaces, enums, annotations) are usually written in CamelCase whereas field names start with a lower case letter (fieldName). While this is not technically a “rule” that the compiler enforces, following this makes your code much more readable to others and also avoids the follow-up problem of hiding the type. Also note that constant fields.

I also made two changes that are good ideas but not really related to your issue:

  • constant values (i.e. most static final fields an enum constants) use ALL_UPPER casing, so I also changed your Breed values
  • I’ve moved the nested type definition to the top of your Dog class so as not to hide it within all the instance fields. This is just to keep those things that logically belong together close to each other.
public class Dog {
    enum Breed { POODLE, SHEPHERD, LABRADOR }

    int age;
    String name;
    Breed breed;

    Dog(int age, String name, Breed breed) {
        this.age = age;
        this.name = name;
        this.breed = breed;
    }
    Dog() {
        this.age = 0;
        this.name = "Rex";
        this.breed = Breed.LABRADOR;
    }
}

Changing all these names in the main class and replacing the unneded new Dow().breed with just Dog.Breed produces this working code:

public class MainClass {
    public static void main(String[] args) {
        Dog randomDog = new Dog();
        Dog myDog = new Dog(4, "Charlie", Dog.Breed.LABRADOR);
        System.out.println("Random dog's name is: " + randomDog.name + ", it's age is: " + randomDog.age + " and it's breed is: " + randomDog.breed);
        System.out.println("My dog's name is: " + myDog.name + ", it's age is: " + myDog.age + " and it's breed is: " + myDog.breed);
    }
}


Source: stackoverflow