Java OOP Basics: Constructors and Packages Explained Clearly

java dev.to

Introduction

In Java, constructors are used to initialize objects, while packages are used to organize classes and interfaces. Both concepts are essential for writing clean, structured, and maintainable programs.

This article covers:

  • Java Packages
  • Default Constructor
  • No-Argument Constructor
  • Parameterized Constructor

What is a Package in Java?

A package in Java is a namespace that groups related classes and interfaces together. It helps in organizing code, avoiding name conflicts, and improving reusability.

Key Points

  • Packages act like folders in a file system
  • They help organize large projects
  • They prevent class name conflicts
  • They support access control

Syntax

package packagename;
Enter fullscreen mode Exit fullscreen mode

Example

package shop;

public class Product {
    String name;
    int price;
}
Enter fullscreen mode Exit fullscreen mode

To compile:

javac -d . Product.java
Enter fullscreen mode Exit fullscreen mode

To run:

java shop.Product
Enter fullscreen mode Exit fullscreen mode

What is a Constructor?

A constructor is a special method that:

  • Has the same name as the class
  • Has no return type
  • Is automatically called when an object is created

Syntax

ClassName() {
    // initialization code
}
Enter fullscreen mode Exit fullscreen mode

Default Constructor

A default constructor is automatically created by the Java compiler if no constructor is defined.

Example

class Demo {
    int x;

    public static void main(String[] args) {
        Demo d = new Demo();
        System.out.println(d.x);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

0
Enter fullscreen mode Exit fullscreen mode

No-Argument Constructor

A no-argument constructor is explicitly defined and does not take any parameters.

Example

class Product {
    String name;
    int price;

    Product() {
        name = "Biscuit";
        price = 20;
    }

    void display() {
        System.out.println(name + " " + price);
    }

    public static void main(String[] args) {
        Product p = new Product();
        p.display();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Biscuit 20
Enter fullscreen mode Exit fullscreen mode

Parameterized Constructor

A parameterized constructor accepts arguments to initialize variables.

Example

class Product {
    String name;
    int price;

    Product(String n, int p) {
        name = n;
        price = p;
    }

    void display() {
        System.out.println(name + " " + price);
    }

    public static void main(String[] args) {
        Product p1 = new Product("Chocolate", 50);
        Product p2 = new Product("Milk", 30);

        p1.display();
        p2.display();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Chocolate 50
Milk 30
Enter fullscreen mode Exit fullscreen mode

Combining Package and Constructors

Here is a complete example using a package with constructors:

package shop;

public class Product {

    String name;
    int price;

    // No-argument constructor
    Product() {
        name = "Default Item";
        price = 0;
    }

    // Parameterized constructor
    Product(String n, int p) {
        name = n;
        price = p;
    }

    void display() {
        System.out.println(name + " " + price);
    }

    public static void main(String[] args) {
        Product p1 = new Product();
        Product p2 = new Product("Chocolate", 50);

        p1.display();
        p2.display();
    }
}
Enter fullscreen mode Exit fullscreen mode

Difference Between Constructors

Feature Default Constructor No-Argument Constructor Parameterized Constructor
Created By Compiler Programmer Programmer
Parameters No No Yes
Flexibility Low Medium High

Common Mistake

Default constructor and no-argument constructor are not the same.

  • Default constructor is automatically provided by the compiler
  • No-argument constructor is written by the programmer

Source: dev.to

arrow_back Back to Tutorials