Constructor:
Zero Argument constructor:
A zero-argument constructor is a constructor that takes no parameters.
You write it explicitly to set up default or custom values for an object.
Example:
class MyClass {
// Zero-argument constructor
MyClass() {
System.out.println("Zero-argument constructor called");
}
}
Default constructor:
- A default constructor is a constructor that Java provides automatically only if you don’t define any constructor in your class.
It:
- Takes no arguments.
- Initializes object with default values (like 0, null, false, etc.).
Example:
public class Student {
int id;
String name;
}
// Java automatically provides a default constructor:
// Student() { }
This:
This Keyword.
The this keyword in Java refers to the current object in a method or constructor.
This keyword is used to differentiate between local and global variable.
When local variable names are the same as instance variables, this is used to differentiate
Example:
public class Student {
String name;
public Student(String name) {
this.name = name;
}
}
Package:
In java packages means folder.
Why use packages?
Avoid class name conflicts.
Make code easier to maintain and understand.
create package:
package com.indianBank;
compile package:
javac -d . classname.java
Run package:
java filename
Top comments (0)