Chapter Notes: Java Reflection


1. Introduction to Java Reflection API

Overview: Java Reflection is a powerful feature that allows a program to inspect and modify its own structure and behavior at runtime. This includes analyzing classes, methods, fields, and constructors, and even changing their values while the program is actively running. This capability is particularly useful for creating dynamic and flexible applications.

Key Uses of Java Reflection:

  1. Runtime Inspection:
  2. Dynamic Behavior:
  3. Frameworks and Libraries:
  4. Tool Development:

Important Note: Reflection is powerful but should be used with caution. It can break the normal rules of the program, leading to potential security risks and performance issues.


2. Java Classes and Type Classes

Java Classes:

Example:

public class Car {
    private String color;
    private String model;
    private int speed;

    public Car(String color, String model, int speed) {
        this.color = color;
        this.model = model;
        this.speed = speed;
    }

    public void drive() {
        System.out.println("The car is driving.");
    }

    public void stop() {
        System.out.println("The car has stopped.");
    }

    // Getters and Setters
    public String getColor() { return color; }
    public void setColor(String color) { this.color = color; }
    public String getModel() { return model; }
    public void setModel(String model) { this.model = model; }
    public int getSpeed() { return speed; }
    public void setSpeed(int speed) { this.speed = speed; }
}

Type Classes in Java: