Ricjssubtil
Ricjssubtil

Reputation: 23

Need to initialize a static final field in the subclasses

I asked a question, but it was very dirty, and a lot of people didn't understand. So, I need to declare a final static field, that is only going to be initialized in the subclasses. I'll show an example:

public class Job {
    public static final String NAME;
}

public class Medic extends Job {

    static {
        NAME = "Medic";
    }
}

public class Gardener extends Job {

    static {
        NAME = "Gardener";
    }
}

Something like this. I know this code is not going to work, since the NAME field in the Job class needs to be initialized. What i want to do is to initialize that field individually in each subclass (Medic, Gardener).

Upvotes: 0

Views: 720

Answers (3)

HungPV
HungPV

Reputation: 489

You need this

public enum Job {
    MEDIC(0),
    GARDENER(1);

    /**
     * get identifier value of this enum
     */
    private final byte value;

    private Job(byte value) {
        this.value = value;
    }

    /**
     * get identifier value of this enum
     * @return <i>int</i>
     */
    public int getValue() {
        return this.value;
    }

    /**
     * get enum which have value equals input string value
     * @param value <i>String</i> 
     * @return <i>Job</i>
     */
    public static Job getEnum(String value) {
        try {
            byte b = Byte.parseByte(value);
            for (Job c : Job.values()) {
                if (c.getValue() == b) {
                    return c;
                }
            }
            throw new Exception("Job does not exists!");
        } catch (NumberFormatException nfEx) {
            throw new Exception("Job does not exists!");
        }
    }

    /**
     * get name of this job
     */
    public String getName() {
        switch (this) {
        case MEDIC:
            return "Medic";
        case GARDENER:
            return "Gardener";
        default:
            throw new NotSupportedException();
        }
    }
}

Upvotes: 1

Tagir Valeev
Tagir Valeev

Reputation: 100349

You cannot do this. The static field has the only once instance per class where it's declared. As both Medic and Gardener share the same Job superclass, they also share the same NAME static field. Thus you cannot assign it twice.

You cannot assign it even once in subclass as it's possible that Job class is already loaded and initialized, but no subclasses are loaded yet. However after class initialization all the static final fields are required to be initialized.

Upvotes: 0

user1438038
user1438038

Reputation: 6079

Why not declare an abstract method in the base class?

public abstract class Job {
   public abstract String getJobName();
}

Then you can return individual names in each implementation:

public class Medic extends Job {
   @Override
   public String getJobName() {
      return "Medic";
   }
}

public class Gardener extends Job {
   @Override
   public String getJobName() {
      return "Gardener";
   }
}

It does not make a lot of sense to have a final static field.

Upvotes: 0

Related Questions