简体   繁体   中英

public static final field in an abstract class or interface

I have a lot of subclasses of an abstract class and each of them declare a public static final field with the same name. I was thinking of having this field in the abstract superclass without initializing it and hoping that each subclass would be forced to initialize it.

I was thinking of this because all of my subclasses of the abstract class declare a public static final String field called UNIQUE_ID, and it is necessary for every subclass to declare such a field with exactly that name.

I hope my question is clear enough, if not please tell me so.

Can something more or less equivalent to this be done?

EDIT: Code added:

My abstract class looks like:

public abstract class ExperimentPanelModel extends Panelizable {
protected String nextButtonText;
protected String backButtonText;
protected String skipButtonText;
protected Properties currentFile;
protected List<Properties> pastFiles = new ArrayList<Properties>();

public ExperimentPanelModel(Properties argcurrentfile, List<Properties> argpastfiles) {
    currentFile = argcurrentfile;
    pastFiles = argpastfiles;
    nextButtonText = "Next";
    backButtonText = "Back";
    skipButtonText = "Skip";
}
...
}

Some of the non-abstract subclasses of that abstract class look like (note that all of them declare public static final String UNIQUE_ID ) :

public class ConfigurationGUI extends ExperimentPanelModel {

public static final String UNIQUE_ID = "ConfigurationGUI";
public static final String DATA_MODIFIED = "DataModified";

Date dateOfLastSession;
int ExperimentalSession;
int ExperimentOrder;

boolean nextButtonEnabled = false;

public ConfigurationGUI(Properties argcurrentfile, List<Properties> argpastfiles) {
    super(argcurrentfile, argpastfiles);
    nextButtonText = "Confirm";
    backButtonText = "Abort";
}

...
}

One example more:

public class Introduction extends ExperimentPanelModel {

public static final String UNIQUE_ID = "Introduction";
public static final String INSTRUCTIONS_XML_FILE = "instructions.xml";
public static final String THIS_INSTRUCTION_PROPERTY = UNIQUE_ID;

private String thisInstructionText = UNIQUE_ID;

Properties readInstructionsProperties = new Properties();

public Introduction(Properties argcurrentfile, List<Properties> argpastfiles) {
 ...

And the last one:

public class Instruction1 extends ExperimentPanelModel {

public static final String UNIQUE_ID = "Instruction1";
public static final String INSTRUCTIONS_XML_FILE = "instructions.xml";
public static final String THIS_INSTRUCTION_PROPERTY = UNIQUE_ID;
...
}

The field idea won't work, because static fields can't be overridden in subclasses. What you can do is you can declare an abstract method on the abstract class so that your subclasses must implement it.

Also note you can't make it a static method because those don't get overridden either.

In your case I would define the variable in the ancestor. No point in having a variable in each of the extending classes, unless you have a particularly good reason, which you don't sound like having.

+1 for Nathan's reply though. In quite a few cases, that's a better thing to do.

Put the public final field UNIQUE-ID in the abstract class and declare a protected constructor which takes the value for UNIQUE-ID. You'll not be able to make it static though as the values are required to be different for different instances.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM