You are on page 1of 16

ENUMS & ANNOTATIONS

Agenda

• What is an enum?
• Why enums?
• What are the annotations?
• How to use annotations?
What is an enum?

• a special data type that enables for a


variable to be one of a set of
predefined constants
=> each constant is unique
An example

public enum Month {


JAN, FEB, MAR, APR,
MAY, JUN, JUL, AUG,
SEP, OCT, NOV, DEC;
}
Enums in other languages

• Most programming languages have enum


types
• C++: values of the constants are values of
an integral type known as the underlying
type of the enum
– This type can be specified
– It defaults to int
Enums in Java

• In Java an enum is a class


• It can have members, methods and
constructors
• BUT:
– The instances are limited: we only have 12
Month instances.
Þ Constructors are private
- All instances are final
- No modifiers are allowed for them
- Members are not final!
Enums in Java

• Q: Can enums be extended?


• A: No
• Q: Do enums have a superclass?
• A: Yes: java.lang.Object
– Remember: in Java everything is a class
• Q: Can enums implement interfaces?
• A: Yes!
Enums in Java

public enum Planet { // example taken from the Oracle site


MERCURY(3.303e+23, 2.4397e6), VENUS(4.869e+24, 6.0518e6), ...;

private final double mass;


private final double radius;
public static final double G = 6.67300E-11;

Planet(double mass, double radius) {


this.mass = mass; this.radius = radius;
}

double surfaceGravity() {return G * mass / (radius * radius);}


double weight(double m) {return m * surfaceGravity();}
}
Why enums?

• Switch blocks for non-numeric types would


disappear => less readable and buggy code:

if (month.equals(“JAN”) || month.equals(“FEB”) || ...) {


System.out.println(“Winter”);
} else if (month.equals(“MAR”) || month.equals(“APR”) || ...) {
System.out.println(“Spring”);
} else if (...) {
System.out.println(“Summer”);
} else {
System.out.println(“Autumn”);
}
Why enums?

• Avoid having a long set of constants in your code


– In a class with lots of constants is hard to figure out
what a constant may mean.
– Instead it's clearer to have the constants grouped
together in several "types" of constants.
Enums In Practice

• Use enums to model the gender field


for an Employee
Annotations

Annotations, a form of metadata, provide


data about a program that is not part of
the program itself.

Annotations have no direct effect on the


operation of the code they annotate.
Annotations

• Java annotations
– Information for the compiler —
Annotations can be used by the compiler to
detect errors or suppress warnings.
– Compile-time and deployment-time
processing — Software tools can process
annotation information to generate code,
XML files, and so forth.
– Runtime processing — Some annotations
are available to be examined at runtime.
Annotations

• Placement of Java annotations:


– Class/interface level
– Method level
– Method parameters level
– Fields level
– Variables level
Annotations

• Built-in Java Annotations


– @Deprecated
– @Override
– @SupressWarnings
Bibliography

• https://docs.oracle.com/javase/tutorial/java
/javaOO/
enum.html
• http://crunchify.com/why-and-for-what-sho
uld-i-use-enum-java-enum-examples
/
• http://www.javapractices.com/topic/TopicAc
tion.do?Id=
1
• https://docs.oracle.com/javase/tutorial/java
/annotations
/

You might also like