Sitemap

Java Class and Objects

4 min readAug 8, 2026

--

So, if I say store age, you will create a variable:

int age = 50;

If I say store name, you will create:

String name = "Lavi";

But what if I say:

Create something where I can store a person’s name, age, and gender together.

Now you might think, okay, I can create separate variables for each of them.

String name = "Lavi";
int age = 22;
String gender = "F";

But here, these variables are still separate.

What if I want to create my own type that represents a Person and contains all these properties together?

That’s where Classes and Objects come in.

A class is basically a user-defined data type or a blueprint/template that defines what data and behavior an object should have.

For example, a Student class can have: name , age , gender

  • And it can also have methods that define what a Student can do.

An object, on the other hand, is an instance of a class. It is the actual entity created from that blueprint.

Think of it like this:

Class = Blueprint
Object = Actual thing created from that blueprint

For example, Student can be a class, while Lavi can be an object of that class.

Press enter or click to view image in full size

Class

  • A user-defined blueprint or template used to create objects.
  • It defines the data (fields) and behavior (methods) that objects will have.
  • It is a logical construct.

For example:

class Student {
int age ;
String name ;
String gender ;
}

Here, Student is our class.

Get Lavi’s stories in your inbox

Join Medium for free to get updates from this writer.

It tells Java that a Student object will have an age, name, and gender.

Object

An object is:

  • An instance of a class.
  • It represents an actual entity created using that class.
  • When an object is created, memory is allocated for the instance’s fields.

For example:

Student myPerson = new Student();
Press enter or click to view image in full size

Now, How Do We Initialize an Object?

Okay, now we have created our object:

Person person = new Person();

But how do we initialize its values?

One way is to assign the values manually:

        myPerson.age = 22;
myPerson.name = "Lavi";
myPerson.gender = "F";

This works but imagine creating 100 objects

We would have to keep assigning values separately for every object.

That can become repetitive and messy.

This is where constructors come into the picture.

Constructor

A constructor is a special member of a class that is used to initialize an object when it is created.

  • Its name must be the same as the class name.
  • It does not have a return type, not even void.

Types of Constructors

We commonly come across these constructor patterns in Java:

  • No-argument constructor — A constructor that doesn’t take any parameters.
  • Parameterized constructor — A constructor that takes parameters to initialize an object with specific values.
  • Overloaded constructor — Multiple constructors in the same class with different parameter lists.
  • Copy constructor — A constructor that creates a new object by copying values from another object of the same class.
  • For example:
class Person {
int age;
String name;
String gender;
    // No-args constructor
// Note: This is NOT the Java-provided default constructor.
// We are explicitly defining a no-argument constructor here.
public Student() {
}

// Parameterized constructor
// Used to initialize the object with specific values.
public Student(int age, String name, String gender) {
this.age = age;
this.gender = gender;
this.name = name;
}

// Specific-field constructor
// Used when we only want to initialize selected fields.
public Student(int age, String name) {
this.age = age;
this.name = name;
}

// Copy constructor
// Used to create a new object by copying values from another Student object.
public Student(Student st) {
this.age = st.age;
this.name = st.name;
this.gender = st.gender;
}Now we can create the object and provide its values directly:
Person person = new Person(22, "Lavi", "F");

Much cleaner.

The constructor gets called automatically when we use new.

This is complete overview of Java class , object and constructor.

Thanks for reading.
Lavi
Java Noob ☕

--

--

Lavi
Lavi

Written by Lavi

Programmers are dumb , I am dumbest.