Defining a Class
A class is defined using the class keyword. It is a template containing fields (attributes) and methods (behaviours).
Anatomy of a Java Class
class Employee {
// โโ Instance variables โโ
int empId;
String name;
double salary;
// โโ Constructor โโ
Employee(int id, String n, double sal) {
empId = id;
name = n;
salary = sal;
}
// โโ Instance method โโ
void display() {
System.out.printf("ID: %d | Name: %s | Salary: %.2f%n", empId, name, salary);
}
// โโ Static method โโ
static void companyName() {
System.out.println("ANITS Engineering College");
}
}Creating Objects
Objects are created using the new keyword. This allocates memory on the heap and calls the constructor.
public class Main {
public static void main(String[] args) {
// Creating objects
Employee e1 = new Employee(101, "Ravi", 45000.0);
Employee e2 = new Employee(102, "Priya", 52000.0);
e1.display();
e2.display();
Employee.companyName(); // static method via class name
}
}e1) is stored on the Stack.Constructors
A constructor is a special method that is automatically called when an object is created. Its purpose is to initialize the object's state.
Rules for Constructors
- Constructor name must be exactly the same as the class name
- Constructor has no return type (not even void)
- Constructor is called automatically when object is created with
new - A constructor can be overloaded
Types of Constructors
| Type | Description |
|---|---|
| Default Constructor | Provided by JVM if no constructor is defined; initializes fields to defaults |
| No-Arg Constructor | Defined by programmer with no parameters |
| Parameterized Constructor | Accepts arguments to initialize fields with specific values |
class Box {
double length, width, height;
// No-arg constructor
Box() {
length = width = height = 1.0;
}
// Parameterized constructor
Box(double l, double w, double h) {
length = l; width = w; height = h;
}
double volume() { return length * width * height; }
}Constructor Overloading
Having multiple constructors in the same class with different parameter lists is called constructor overloading. It provides flexibility in object creation.
class Student {
int rollNo;
String name;
double cgpa;
Student() { // No-arg
rollNo = 0; name = "Unknown"; cgpa = 0.0;
}
Student(int r, String n) { // 2-arg
rollNo = r; name = n; cgpa = 0.0;
}
Student(int r, String n, double g) { // 3-arg
rollNo = r; name = n; cgpa = g;
}
void show() {
System.out.printf("%d | %s | %.2f%n", rollNo, name, cgpa);
}
}
public class Test {
public static void main(String[] args) {
new Student().show();
new Student(101, "Arjun").show();
new Student(102, "Priya", 9.2).show();
}
}Method Overloading
Method Overloading = same method name, different parameter list. The compiler differentiates them based on number, type, or order of parameters.
class MathUtil {
static int square(int n) { return n * n; }
static double square(double n) { return n * n; }
static int add(int a, int b) { return a + b; }
static int add(int a, int b, int c) { return a + b + c; }
static double add(double a, double b) { return a + b; }
}Parameter Passing
Java always uses Pass by Value.
- For primitives: a copy of the value is passed โ changes inside the method do NOT affect the original.
- For objects: the reference (address) is passed by value โ changes to the object's fields inside the method DO affect the original object, but reassigning the reference does not.
void changeVal(int x) { x = 100; } // original unchanged
void changeObj(Box b) { b.length = 99; } // original object IS changedthis Keyword (intro)
The this keyword refers to the current object. Use it when a local variable shadows an instance variable:
class Circle {
double radius;
Circle(double radius) {
this.radius = radius; // 'this.radius' = instance var, 'radius' = param
}
double area() { return Math.PI * this.radius * this.radius; }
}this() can also be used to call one constructor from another (constructor chaining). It must be the first statement in the constructor.Download PDF Notes
Save these notes as a PDF for offline reference