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;
Example
package shop;
public class Product {
String name;
int price;
}
To compile:
javac -d . Product.java
To run:
java shop.Product
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
}
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);
}
}
Output:
0
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();
}
}
Output:
Biscuit 20
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();
}
}
Output:
Chocolate 50
Milk 30
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();
}
}
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