Sunday, May 10, 2026

JAVA_Chapter2_INTRODUCING CLASSES, OBJECTS AND METHODS

2.1 Class Fundamentals

Class

A class is a blueprint/template used to create objects.

Syntax

class Student{
   int id;
   String name;
}

Explanation

  • class keyword is used to define class.
  • Variables inside class are called data members.
  • Functions inside class are called methods.

Advantages of Class

  • Code reusability
  • Real world modeling
  • Easy maintenance
  • Security

2.2 Object Creation

Object

An object is an instance of class.

Syntax

Student s = new Student();

Explanation

Part Meaning
Student class type
s reference variable
new allocates memory
Student() constructor call

Accessing Members

s.id = 1;
s.name = "Ram";

System.out.println(s.name);

Complete Example of Class and Object

class Student{
   int id;
   String name;

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

   public static void main(String args[]){

      Student s1 = new Student();

      s1.id = 1;
      s1.name = "Ram";

      s1.display();
   }
}

2.3 Methods

Method

A method is a block of code that performs a specific task.

Syntax

returntype methodname(){
}

Example

void show(){
   System.out.println("Hello");
}

Method Types

Type Description
Method with no return and no argument simple display method
Method with argument receives value
Method with return type returns value

Method with Arguments

void add(int a, int b){
   System.out.println(a+b);
}

Method with Return Type

int add(int a,int b){
   return a+b;
}

Method Calling

obj.add();

Call by Value

Java uses call by value.

void change(int x){
   x = 100;
}

Original value does not change.


2.4 Command Line Arguments

Definition

Values passed during program execution are called command line arguments.

Syntax

public static void main(String args[])

Example

class Test{
   public static void main(String args[]){

      System.out.println(args[0]);
   }
}

Execution

java Test Ram

Output

Ram

2.5 Constructors

Constructor

A constructor is a special method used to initialize object.

Properties of Constructor

  • Same name as class
  • No return type
  • Automatically called
  • Used during object creation

Default Constructor

class Demo{

   Demo(){
      System.out.println("Constructor");
   }
}

Parameterized Constructor

class Student{

   int id;

   Student(int i){
      id = i;
   }
}

Constructor Overloading

class Test{

   Test(){
   }

   Test(int x){
   }
}

Multiple constructors with different parameters.


Difference Between Constructor and Method

Constructor Method
same name as class any valid name
no return type has return type
called automatically called manually
used to initialize object used to perform task

2.6 Garbage Collection

Definition

Garbage collection destroys unused objects automatically.

Purpose

  • Free memory
  • Prevent memory leak
  • Improve performance

Garbage Collector

JVM automatically performs garbage collection.

Requesting Garbage Collection

System.gc();

Finalize Method

protected void finalize(){
   System.out.println("Destroyed");
}

2.7 this Keyword

Definition

this refers to current object.

Uses of this Keyword

  • Differentiate instance variable and local variable
  • Invoke current class method
  • Invoke current class constructor
  • Pass current object

Example

class Student{

   int id;

   Student(int id){
      this.id = id;
   }
}

this() Constructor Call

class A{

   A(){
      this(10);
   }

   A(int x){
      System.out.println(x);
   }
}

2.8 Static Fields and Methods

Static Variable

Shared by all objects.

class Test{

   static int count = 0;
}

Static Method

static void show(){
   System.out.println("Hello");
}

Calling Static Method

Test.show();

Properties of Static

  • Belongs to class
  • No need to create object
  • Memory efficient

Important Rule

Static method cannot directly access non-static members.


Static Block

class Test{

   static{
      System.out.println("Static Block");
   }
}

Executes before main().


2.9 Nested and Inner Classes

Nested Class

Class inside another class.

Syntax

class Outer{

   class Inner{
   }
}

Advantages

  • Better grouping
  • Improved encapsulation
  • Readable code

Creating Inner Class Object

Outer obj = new Outer();
Outer.Inner in = obj.new Inner();

2.10 Variable Length Arguments (Varargs)

Definition

Allows method to accept variable number of arguments.

Syntax

void add(int ... x){
}

Example

class Test{

   static void sum(int ... n){

      int s = 0;

      for(int x : n){
         s = s + x;
      }

      System.out.println(s);
   }

   public static void main(String args[]){

      sum(1,2);
      sum(1,2,3,4);
   }
}

Enhanced for Loop (for-each)

for(int x : arr){
   System.out.println(x);
}

Most Important Practical Programs

  1. Class and Object program
  2. Method with argument and return type
  3. Constructor program
  4. Constructor overloading
  5. Command line argument program
  6. Static variable and static method
  7. this keyword program
  8. Inner class program
  9. Varargs program

Most Important Theory Questions

  • Define class and object.
  • Explain constructor with example.
  • Difference between constructor and method.
  • Explain static keyword.
  • What is garbage collection?
  • Explain this keyword.
  • What are nested classes?
  • Explain command line arguments.
  • Explain variable length arguments.

Important Viva Questions

Why constructor has no return type?

Because constructor initializes object, not return value.

Can constructor be static?

No.

Can we overload constructor?

Yes.

Can we override constructor?

No.

Why main() is static?

Because JVM calls it without creating object.

Can static method access instance variable?

No, directly cannot.

Difference Between Static and Non-static

Static Non-static
belongs to class belongs to object
shared memory separate memory
object not required object required

0 comments:

Post a Comment