0

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).

  • `static final` won't make much sense here. Just create an instance variable. – Suresh Atta Sep 14 '15 at 09:31
  • 1
    Why won't you create create abstract static method `getName()`? – Crozin Sep 14 '15 at 09:31
  • 1
    @Crozin Abstract static methods? Not possible: http://stackoverflow.com/a/370967/1743880 – Tunaki Sep 14 '15 at 09:33
  • I dont want to create instance variables, since I don't want to create new instances just to get a final field. I also don't wanted to create methods, because I wanted to find a solution without defining new methods. – Ricjssubtil Sep 14 '15 at 09:36
  • @Ricjssubtil can you give an example of how do you want to access the final field? – JohnnyAW Sep 14 '15 at 09:37
  • I think you need the ENUM: Job.MEDIC, Job.GARDENER (https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html) – HungPV Sep 14 '15 at 09:41

3 Answers3

1

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();
        }
    }
}
HungPV
  • 489
  • 6
  • 19
0

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.

user1438038
  • 5,821
  • 6
  • 60
  • 94
0

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.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334