Here': Example
Here': Example
Enumerations (Enums)
Description: Enumerations, or enum types, are a special type of class in Java that represents
a group of constants. Enums are particularly useful when a variable can take one of a small
set of predefined values, such as days of the week, directions, or colors. Each constant in an
enum is a public, static, and final instance, which means that it is immutable and cannot be
modified.
Syntax:
enum EnumName {
CONSTANT1, CONSTANT2, CONSTANT3, ...
}
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
Example:
3. Annotations in Java
Description: Annotations are a form of metadata added to Java code, primarily used to
provide information to the compiler, influence runtime behavior, or help with code
documentation. They don't change the code’s execution by themselves but can be
processed by tools, frameworks, or the compiler to add extra functionality. Java provides
several built-in annotations like @Override, @Deprecated, and @SuppressWarnings, but
custom annotations can also be created.
Built-in Annotations:
@Override: Indicates that a method overrides a method from a superclass. This helps the
compiler catch errors if the method signature doesn't match exactly.
@Deprecated: Marks a method or class as outdated, signaling to developers that a newer
version is preferred.
@SuppressWarnings: Suppresses specified compiler warnings, such as unchecked
operations or deprecated API usage.
Custom Annotations: Java also allows creating custom annotations, which can be used for
various purposes, like configuring methods for specific frameworks or for defining metadata
to be used at runtime.
Creating a Custom Annotation: A custom annotation can be defined with @interface.
Optionally, elements can be added to annotations, similar to attributes in a class. These
elements can have default values.
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Using Custom Annotations: Here’s an example of how to apply the custom annotation.
if (annotation != null) {
System.out.println("Author: " + annotation.author());
System.out.println("Date: " + annotation.date());
System.out.println("Description: " + annotation.description());
}
}
}
Explanation:
@MyAnnotation is applied to the annotatedMethod() method with specific values for each
element.
We use reflection (getMethod().getAnnotation()) to access the annotation and its values at
runtime.
Output:
4. Generics
Description: Generics provide a way to create classes, interfaces, and methods with type
parameters. They allow code to be type-safe and reusable. For example, rather than writing
separate classes to handle Integer and String types, you can create a generic class that can
handle any type.
Syntax:
class ClassName<T> {
// Code using T
}
Example: Here’s a generic class Box that can hold any type:
class Box<T> {
private T content;
public T getContent() {
return content;
}
}
Explanation: In this example, Box is a generic class with a type parameter T. This means Box
can store any type of data. We created Box objects to hold an Integer and a String,
demonstrating the flexibility of generics.