DEV Community

Base Java Part 2 (Class vs Object)

This is part of the Base Java series, in this part 2 we discuss

  1. Difference between Class & Object.
  2. How to create an object in Java.
  3. Talk about constructor in Java.
  4. Different ways to create an object in Java.

Here is the full video of the part 2 Base Java Series. Please show some love and subscribe to my channel Subscribe.


A class is a template or a set of instructions to build a specific type of object, it determines how an object behaves and what the object contains.


Objects represent real-life entities, Object determines the behavior of the class, the object has a state, behavior, and identity. Object implements the state and behavior in the form of variables and methods and requires some memory allocated.

Let's take the example, you want to build a car management system

You will define car class which will have a name, type, color, and functions like running, autopilot, stopping, etc.

The objects are tesla, accord, Lexus, etc.. as shown in the below image

Class Object

Different ways to create an Object

We create an object in Java in multiple ways

  1. "new" keyword.
  2. "newInstance()" method.
  3. using "Clone()" method.
  4. using "ClassLoader".

The below Car Class and SampleJavaProject Class demonstrates these 4 ways to create Objects in Java.


public class Car implements Cloneable{

    private String Name;

    private String Type;

    private String Color;

    public Car() {

    protected Object clone() throws CloneNotSupportedException {
        return super.clone();

    public  Car(String name, String type, String color) {
        this.Name = name;
        this.Type = type;
        this.Color = color;

    public String toString() {
        return "Car [Name=" + Name + ", Type=" + Type + ", Color=" + Color + "]";

    public String getName() {
        return Name;

    public void setName(String name) {
        Name = name;

    public String getType() {
        return Type;

    public void setType(String type) {
        Type = type;

    public String getColor() {
        return Color;

    public void setColor(String color) {
        Color = color;



public class SampleJavaProject {

    public static void main(String [] args) {

        Car accord = new Car("Accord","2WD","Blue");
        Car tesla = new Car("Tesla","4WD","White");
        Car camry = new Car("Camry","2WD","red");
        try {
        Class clsClass = Class.forName("");
        Car tesla2 = (Car)clsClass.newInstance();
        }catch(Exception e) {
        try {
        Car obj1 = new Car();
        Car obj2 = (Car) obj1.clone();
        System.out.println("clone car----->"+obj2.toString());
        } catch (Exception e) {

        Car classLoaderObjeCar = null;
        try {
            classLoaderObjeCar = (Car) new SampleJavaProject().getClass().getClassLoader().loadClass("").newInstance();
            classLoaderObjeCar.setName("classloader car");
            System.out.println("Class Loader car----->"+classLoaderObjeCar.toString());
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            // TODO Auto-generated catch block

Here is the full video of the part 2 Base Java Series. Please show some love and subscribe to my channel Subscribe.

Top comments (0)