DEV Community

Cover image for What Really Happens When You Create an Object in Java? Understanding Constructors and Packages
Hariharan S J
Hariharan S J

Posted on

What Really Happens When You Create an Object in Java? Understanding Constructors and Packages

1.Introduction

Have you ever created an object in Java and wondered what happens behind the scenes the moment that object comes to life?

When you write something as simple as:

Student s1 = new Student();
Enter fullscreen mode Exit fullscreen mode

Java doesn't just magically create an object. It first invokes a special mechanism called a constructor, which prepares the object and gets it ready for use. Whether you're assigning default values or passing custom data during object creation, constructors play a crucial role in every Java application.

But creating objects is only part of the story.

As applications grow from a few classes to hundreds or even thousands, managing code becomes a challenge. Imagine trying to find a single class in a project where every file is stored in the same folder. That's where packages come in. Packages help developers organize code, avoid naming conflicts, and build scalable applications that are easier to maintain.

In this article, we'll explore the different types of constructors in Java, understand how they work, and learn how packages help structure real-world Java projects. By the end, you'll have a solid understanding of two fundamental concepts that every Java developer uses daily.

In my previous blog i had discussed what is constructor? , why constructor is needed in this blog we are going to discuss about the Types of Constructor and Packages

2.Types of Constructors in Java

Java mainly provides three types of constructors:

  1. Default Constructor

  2. No-Argument Constructor

  3. Parameterized Constructor

1. Default Constructor

A default constructor is automatically provided by the Java compiler when no constructor is explicitly written.

If we do not create any constructor, the Java compiler automatically creates a no-arg constructor during the execution of the program.

This constructor is called the default constructor.

Example

class Student {

    int id;
    String name;

    public static void main(String[] args) {

        Student s1 = new Student();

        System.out.println(s1.id);
        System.out.println(s1.name);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

0
null
Enter fullscreen mode Exit fullscreen mode

Explanation

Since no constructor was defined, Java automatically generated a default constructor and assigned default values to instance variables.

2. No-Argument Constructor

A no-argument constructor is created by the programmer and does not accept any parameters

Similar to methods, a Java constructor may or may not have any parameters (arguments).

If a constructor does not accept any parameters, it is known as a no-argument constructor. For example,

Example

class Student {

    int id;
    String name;

    Student() {
        id = 101;
        name = "Hari";
    }

    public static void main(String[] args) {

        Student s1 = new Student();

        System.out.println(s1.id);
        System.out.println(s1.name);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

101
Hari
Enter fullscreen mode Exit fullscreen mode

Why Use It?

A no-argument constructor allows us to initialize objects with custom default values instead of Java's built-in defaults.

3. Parameterized Constructor

A parameterized constructor accepts values during object creation.

A Java constructor can also accept one or more parameters. Such constructors are known as parameterized constructors (constructors with parameters).

Example

class Student {

    int id;
    String name;

    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public static void main(String[] args) {

        Student s1 = new Student(101, "Hari");

        System.out.println(s1.id);
        System.out.println(s1.name);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

101
Hari
Enter fullscreen mode Exit fullscreen mode

Why Use It?

Parameterized constructors allow each object to have different values during creation.

Student s1 = new Student(101, "Hari");
Student s2 = new Student(102, "John");
Student s3 = new Student(103, "David");
Enter fullscreen mode Exit fullscreen mode

Each object stores unique data.

Constructor Overloading

Java allows multiple constructors within the same class as long as their parameter lists are different.

Example

class Student {

    Student() {
        System.out.println("No Argument Constructor");
    }

    Student(String name) {
        System.out.println("Parameterized Constructor");
    }

    public static void main(String[] args) {

        Student s1 = new Student();
        Student s2 = new Student("Hari");
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

No Argument Constructor
Parameterized Constructor
Enter fullscreen mode Exit fullscreen mode

This feature is known as Constructor Overloading.

3.What is a Package in Java?

A package is a collection of related classes, interfaces, and sub-packages.

Think of a package as a folder in your computer.

Just as folders help organize files, packages help organize Java classes.

A package in Java is a mechanism to group related classes, interfaces, and sub-packages into a single unit. Packages help organize large applications, avoid naming conflicts, provide access protection, and make code modular and maintainable.

  • Avoiding name conflicts (two classes with the same name can exist in different packages)

  • Providing access control using public, protected, and default access

  • Reusability: packaged code can be imported and used anywhere

  • Encouraging modular programming

4.Why Do We Need Packages?

Packages provide several advantages:

1. Better Code Organization

Without packages:

Student.java
Employee.java
Customer.java
Order.java
Enter fullscreen mode Exit fullscreen mode

With Packages

com.company.student
com.company.employee
com.company.order
Enter fullscreen mode Exit fullscreen mode

Everything becomes easier to manage.

2. Prevent Naming Conflicts

Two developers might create classes with the same name.

Example:

student.Student
employee.Student

Packages help Java identify which class is being used.

3. Access Protection

Packages help control visibility of classes and members using access modifiers.

Types of Packages

Java provides two types of packages:

1. Built-in Packages

These are packages provided by Java itself.

Examples:

java.lang(TBD)
java.util(TBD)
java.io(TBD)
java.sql(TBD)
Enter fullscreen mode Exit fullscreen mode

2. User-Defined Packages

Developers can create their own packages.

Creating a Package

package college;

public class Student {

    public void display() {
        System.out.println("Inside Student Class");
    }
}
Enter fullscreen mode Exit fullscreen mode

Using the Package

import college.Student;

public class Main {

    public static void main(String[] args) {

        Student s = new Student();

        s.display();
    }
}
Enter fullscreen mode Exit fullscreen mode

Package Naming Convention

Java package names usually follow reverse domain naming.

Example:

com.google
com.microsoft
com.amazon
org.apache
Enter fullscreen mode Exit fullscreen mode

This helps avoid naming conflicts in large applications.

5.Final Thoughts

Constructors and packages may seem like small Java concepts at first, but they play a huge role in real-world application development.

Constructors ensure that objects are properly initialized when created, while packages help organize code into meaningful structures.

As your Java projects become larger and more complex, understanding these two concepts will help you write cleaner, more maintainable, and professional-quality code.

Master constructors to control object creation. Master packages to control project organization. Together, they form a strong foundation for becoming an effective Java developer.

Reference

(https://www.geeksforgeeks.org/java/packages-in-java/)

(https://www.programiz.com/java-programming/constructors)

Top comments (0)