2

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?

Community
  • 1
  • 1
Luke Taylor
  • 9,481
  • 13
  • 41
  • 73
  • If you define the constants directly there is always the possiblity of error. What about using GUIDs? – SJuan76 Jan 20 '13 at 18:05

3 Answers3

3

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.

Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
  • (obsolate) Although this answers the question, keep in mind what artragis said about not being able to access the private fields of the base class. – Vincent Jan 20 '13 at 18:09
  • Hmm... And what about if somebody will insert new value into `Base` class? In this case all offsping's constants will increase by `1`. And what if this constants are used in some DB tables as special state values? – Andremoniy Jan 20 '13 at 18:24
  • Thank you, this seems permissible. Sorry, the fields should be public. – Luke Taylor Jan 20 '13 at 18:31
  • @Andremoniy: Yes, true. It seems dangerous, but i infer from the question that that is the desired behaviour. – Tom Anderson Jan 20 '13 at 18:58
  • I like the idea. However, what would you do if you have a class that extends the "OffSpring" class? – Luke Taylor Feb 10 '13 at 17:24
  • @LukeTaylor: You'd use the same mechanism. Again, that works as long as there is only one class that extends `Offspring`, and no class which extends `Base`. – Tom Anderson Feb 10 '13 at 22:39
  • There are ways you could get around that constraint. For example, you could give each class a base error number of its own (allocated manually, probably), and then make every error code an offset from that base. If the bases were 100, 200, 300, etc, then each class would have a range of a hundred error numbers it could safely use. But all this leads me back to my initial point, which is that this really, really doesn't seem like these are truly private values, and once you accept they are public values, you have a lot of better options for dealing with them. – Tom Anderson Feb 10 '13 at 23:03
2

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++; 
    }
}
artragis
  • 3,677
  • 1
  • 18
  • 30
1

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.

DarthVader
  • 52,984
  • 76
  • 209
  • 300