Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
...
}
like image 680
deinocheirus Avatar asked Mar 11 '13 16:03

deinocheirus


3 Answers

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.

like image 190
Nathan Hughes Avatar answered Sep 22 '22 12:09

Nathan Hughes


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.

like image 38
carlspring Avatar answered Sep 19 '22 12:09

carlspring


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.

like image 29
GHC Avatar answered Sep 21 '22 12:09

GHC