Luke Taylor
Luke Taylor

Reputation: 9599

OOP Design: Static fields

Currently I declare all my static fields like so:

public static final int EXAMPLE_OF_STATIC_FIELD = 0;

Say I have a base class with fields that represent all errors that could ocurre inside the class:

public class Base {

    public static final int ERROR_1 = 0;
    public static final int ERROR_2 = 1;
    public static final int ERROR_3 = 2;

}

If I extend this base class and I would like to add more Error types to the class, I would do the following:

public class OffSpring extends Base {

        public static final int NEW_ERROR_1 = 3;

    }

For me to declare new Error types, I need to know the value of the Error types from the base class, which (in my opinion) in not very convenient, since I could accidentally declare an Error Type in an offspring class of the Base class with the same value as an Error type from the Base class. For example:

public static final int NEW_ERROR_1 = 0;

which would be the same as

public static final int ERROR_1 = 0;

which would conflict...

I thought of maybe using an Enum Class, however it turns out that you can't extend it. Can enums be subclassed to add new elements?

Another option would be to use String value type instead of int value type for all the static fields, but this is not a very efficient solution...

How can I add more fields to an offspring of a class, without them conflicting with the super class?

Upvotes: 2

Views: 164

Answers (3)

Tom Anderson
Tom Anderson

Reputation: 47243

I would question whether you really want to do this. If the constants are private to their declaring classes, why does it matter if they reuse the same values? If there is a context where it is important that different errors do not use the same values, then that suggests strongly that they should not actually be private.

If you do actually want to do this, you could do something like this:

public class Base {
    private static final int ERROR_1 = 0;
    private static final int ERROR_2 = 1;
    private static final int ERROR_3 = 2;
    protected static final int LAST_ERROR = ERROR_3;
}

public class OffSpring extends Base {
    private static final int NEW_ERROR_1 = Base.LAST_ERROR + 1;
}

If you want to have multiple subclasses of Base which each define their own error codes, though, you're in trouble.

Upvotes: 3

artragis
artragis

Reputation: 3713

I can't see your problem as private static final are not inherited attributes. You will not be able to access to OffSpring.ERROR_1it does not exist. So if you want to use error you have the choice to use enum and use them in the proper way. If you do prefere integers it is because you want to use hacks like error combination so you can't use the value 3.

What do I call error Combination? Imagin your script fails because of two errors, ERROR_1 and Error_2 if ERROR_1 is 1 and ERROR_2 is 2 if you say you've got an error with code 3 you say you have got ERROR_1 and ERROR_2 as 1 is binary coded as 01 and 2 as 10. 3 is the combination of them two : 11.

If you want unique identification for all custom errors, use a GUID generator, for example

public class GUIDGenerator{
    private int id = 1;
    public int getId(){
          return id++; 
    }
}

Upvotes: 2

DarthVader
DarthVader

Reputation: 55112

Why dont you store your error types in a map so that you can check if the error type is declared or not because that is what you want to do.

Have a Map<String, int> error_types. When you keep adding more you can have a check at the base class and avoid collisions.

Upvotes: 1

Related Questions