Leela Soft Core Java (J2SE) Madhusudhan
3. Constructors
Constructors: Special methods used to initialize objects. They have the same name as the class
and do not have a return type. Constructors can be overloaded to provide different ways to
initialize objects.
public class Person {
String name;
int age;
// Constructor
Person(String name, int age) {
[Link] = name;
[Link] = age;
}
}
4. Nested Classes
Static Nested Classes: A nested class defined with the static keyword. It can access the static
members of the outer class.
public class Outer {
static class Nested {
void display() {
[Link]("Inside static nested class");
}
}
}
Non-Static Nested Classes (Inner Classes): These classes can access all members of the outer
class, including private members.
public class Outer {
class Inner {
void display() {
[Link]("Inside inner class");
}
}
}
5. Enums
Enum Types: Special class that represents a group of constants. They are used to define a
collection of constants.
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
[Link]@[Link] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
Declaration of Class Objects:
• A class provides the blueprint for objects; we create an object from a class.
• The act of specifying a variable's data type and name is known as the declaration of
an object's state, which is also referred to as variable declaration.
Example of a Class:
public class Car {
String color;
String model;
int year;
public Car(String c, String m, int y) {
color = c;
model = m;
year = y;
}
}
From the above class;
Car c1 = new Car("Blue", "Benz", 2004);
Car c2 = new Car("Red", "Maruti", 2005);
These lines create an objects of the Car class
Each of these statements has three parts:
• Declaration: The code set in bold are all variable declarations that associate a variable
name with an object type.
• Instantiation: The new keyword is a Java operator that creates the object.
• Initialization: The new operator is followed by a call to a constructor, which initializes
the new object.
Assigning One Object to Another:
• In Java, when we assign one object to another, we are copying the reference of the
object, not the actual object itself.
• This means that both variables will point to the same object in memory.
• Any changes made to the object through one reference will be reflected in the other,
since they both reference the same underlying object.
Example: Assigning One Object to Another
class Car {
String model;
String color;
Car(String model, String color) {
[Link] = model;
[Link]@[Link] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
[Link] = color;
[Link]@[Link] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
}
void displayInfo() {
[Link]("Model: " + model + ", Color: " + color);
}
}
public class Test {
public static void main(String[] args) {
// Creating a Car object
Car car1 = new Car("Toyota", "Red");
// Assigning car1 to car2
Car car2 = car1;
// Displaying car1 information
[Link]("Car1:");
[Link]();
// Displaying car2 information
[Link]("Car2:");
[Link]();
// Modifying car2
[Link] = "Blue";
// Displaying both objects again after modification
[Link]("After modification:");
[Link]("Car1:");
[Link]();
[Link]("Car2:");
[Link]();
}
}
Output:
Car1:
Model: Toyota, Color: Red
Car2:
Model: Toyota, Color: Red
After modification:
Car1:
Model: Toyota, Color: Blue
Car2:
Model: Toyota, Color: Blue
Explanation:
• Object Creation: car1 is created with the model "Toyota" and color "Red".
[Link]@[Link] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
• Object Assignment: car2 is assigned the reference of car1. Now, both car1 and car2
refer to the same object in memory.
• Modification: Changing the color of car2 to "Blue" also changes the color for car1 since
both references point to the same object.
• Output: Both car1 and car2 will display "Model: Toyota, Color: Blue" after the
modification.
Access Control for Class Members:
• In Java, access control for class members (fields, methods, constructors, and inner
classes) is managed using access modifiers.
• These modifiers determine the visibility and accessibility of the members within
different parts of the program.
• There are FOUR access modifiers in Java: private, default, protected and
public.
1. Public (public)
• Visibility: The member is accessible from any other class, regardless of the package.
• Usage: Use public for methods and fields that need to be accessible from outside the
class or package.
Example:
//[Link] (File Name)
package sample1;
public class Employee {
public void display() {
[Link]("Welcome");
}
}
//[Link] (File Name)
package sample2;
import sample1;
class Test {
public static void main(String args[]) {
Employee emp = new Employee();
[Link]();
}
}
Output: Welcome
2. Protected (protected)
[Link]@[Link] Cell: 78 42 66 47 66