Java Program to Determine the class of an Object

Java Program to Determine the class of an object

What is class in Java ?

In Java, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or instance variables) and implementations of behavior (member functions or methods).

Classes define objects and the methods that can be used on or by that object. Objects are instances of a class, and they can contain data and methods that operate on that data.

What is Object in Java ?

An object in Java is an instance of a class. It is a unique entity that represents the class and contains its own state and behavior. The state of an object is represented by the values of its instance variables and the behavior of an object is represented by its methods.

An object is created from a class, and you can create multiple objects from a single class. Each object created from a class is unique and operates independently from any other object created from the same class.

Sysntax :

Car myCar = new Car("Toyota", "Camry", 2020);

Ways to Determine the class of Object  : 

Example : using instanceof operator

Run
class Car {
  private String make;
  private String model;
  private int year;

  public Car(String make, String model, int year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }

  public String getMake() {
    return make;
  }

  public String getModel() {
    return model;
  }

  public int getYear() {
    return year;
  }
}

class Main {
  public static void main(String[] args) {
    Car myCar = new Car("Toyota", "Camry", 2020);

    if (myCar instanceof Car) {
      System.out.println("myCar is an instance of Car.");
    } else {
      System.out.println("myCar is not an instance of Car.");
    }
  }
}

Output :

myCar is an instance of Car.

Example : using getClass() Method

Run

class Car {
  private String make;
  private String model;
  private int year;

  public Car(String make, String model, int year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }

  public String getMake() {
    return make;
  }

  public String getModel() {
    return model;
  }

  public int getYear() {
    return year;
  }
}

class Main {
  public static void main(String[] args) {
    Car myCar = new Car("Toyota", "Camry", 2020);

    Class carClass = myCar.getClass();
    System.out.println("The class of myCar is: " + carClass.getName());
  }
}

Output :

The class of myCar is: Car

Example : Using Class.forName() method

Run

class Car {
  private String make;
  private String model;
  private int year;

  public Car(String make, String model, int year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }

  public String getMake() {
    return make;
  }

  public String getModel() {
    return model;
  }

  public int getYear() {
    return year;
  }
}

class Main {
  public static void main(String[] args) {
    try {
      Class carClass = Class.forName("Car");
      System.out.println("The class of Car using Class.forName() is: " + carClass.toString());
    } catch (ClassNotFoundException e) {
      System.out.println("Class not found: " + e.getMessage());
    }
  }
}

Output :

The class of Car using Class.forName() is: class Car

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription