Enums and Annotations Flashcards

1
Q

Explain why enums are preferable to static int fields.

A
  • With java 1.5, enum type is introduced which completely supercede static int fields.
  • Traditionally, if you have certain set of things you would like to represent, you do this with a static int fields. Unfortunately, this is not typesafe, there’s no way to iterate through all the values, and you can’t print a string friendly version of the static int fields.
  • Enum solves all this problems and provide more. You can think of enum as a class that exposes a few static final fields, with each field being instance controlled so that there is only one of instance of each field.
  • Because enums really are classes, you can store variables specific to each enum and define methods for each enum to implement just like real classes. This makes enum much more flexible than static int fields.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain what you can do if there’s a need to extend an enum.

A
  • You cannot extend enums directly, but if you provide a library and want client to be able to provide new enums, you can declare an interface, have your enum implement that interface, and use that interface in your api.
  • If client wants to provide new enum types, they can then just implement that interface and provide new enums.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain what annotations are and why it should be used in preference to name conventions.

A
  • Prior to java 1.5, toolsmiths rely on naming conventions and reflection on these naming conventions when the tool tries to do something with the code.
  • There are a few problems with this approach: 1) No warning if client slightly misspelled a convention 2) No warning if client try to use the convention on the wrong thing like class instead of method 3) No easy way to pass parameters with naming conventions.
  • Annotations solve all these problems. Annotion names are checked during compiled time so it can’t be misspelled. You can specify which type of construct the annotation applied to with @Target, and you can pass parameters with annotations.
  • For toolsmiths, always use annotations and reflection on the annotation instead of naming conventions.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain when to use marker interfaces and when to use marker annotations.

A
  • Marker interfaces are interfaces with no method used to specify that an object can be used to do something.
  • Most of the time, marker interfaces should be preferred over marker annotations because 1) You can have methods accept the marker interface as its type, enforcing the contract 2) You can have the marker interface extends some other interface if the marker interface apply only to that type 3) All of this can be checked at compile time.
  • If you have no need for these things, you may consider marker annotations, but only then.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly