Classes in Java
A class in Java is a blueprint or template for creating objects.
It defines the data (attributes or fields) and the methods (functions or behaviors) that the objects created from the class can have.
Think of a class as a blueprint for a house.
Just as a blueprint defines the structure and features of the house, a class defines the structure and features of objects.
Basic Structure of a Class
A Java class typically contains:
Fields (Attributes): Variables that hold the data or state of an object.
Methods: Functions that define the behavior or actions that an object can perform.
Constructors: Special methods used to initialize objects.
Example: Defining a Simple Class
Let’s look at an example to understand how to define a class in Java.
class Car {
// Fields (Attributes)
String color;
String model;
int year;
// Constructor
public Car(String color, String model, int year) {
this.color = color;
this.model = model;
this.year = year;
}
// Method
public void displayDetails() {
System.out.println("Car Model: " + model);
System.out.println("Car Color: " + color);
System.out.println("Car Year: " + year);
}
}
Explanation:
Class Declaration: class Car declares a class named Car.
Fields: String color, String model, and int year are fields that store the data of the object.
Constructor: public Car(String color, String model, int year) is a constructor that initializes the fields when an object is created.
Method: public void displayDetails() is a method that prints the details of the car.
Creating Objects from a Class
Once a class is defined, you can create objects (instances) of that class. Here’s how you create and use an object of the Car class:
public class Main {
public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car("Red", "Toyota", 2020);
// Calling the method
myCar.displayDetails();
}
}
Explanation:
Creating an Object: Car myCar = new Car("Red", "Toyota", 2020); creates an instance of the Car class and initializes it with the given values.
Calling a Method: myCar.displayDetails(); calls the displayDetails method to print the car’s details.
The output will be:
Car Model: Toyota
Car Color: Red
Car Year: 2020
Fields in Detail
Fields (or attributes) are variables that hold the data associated with a class and its objects. They represent the state or properties of an object.
Example:
class Person {
String name;
int age;
}
In this example, Person has two fields: name and age.
Methods in Detail
Methods define the behavior or actions that an object of the class can perform. They are functions defined within a class.
Example:
class Person {
String name;
int age;
// Method to display person's details
public void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
In this example, displayDetails is a method that prints the person's details.
Constructors in Detail
Constructors are special methods used to initialize objects. They have the same name as the class and do not have a return type.
Example:
class Person {
String name;
int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
In this example, Person has a constructor that initializes the name and age fields.
Access Modifiers
Access modifiers define the visibility of fields, methods, and constructors. The most common access modifiers are:
public: The member is accessible from any other class.
private: The member is accessible only within the class it is declared.
protected: The member is accessible within the package and by subclasses.
default (no modifier): The member is accessible only within the package.
Example:
class Person {
private String name; // private field
private int age; // private field
// public constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// public method
public void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
In this example, the fields name and age are private, meaning they cannot be accessed directly from outside the class.
The constructor and displayDetails method are public, meaning they can be accessed from outside the class.
Summary
A class in Java is a blueprint for creating objects. It defines attributes (fields) and behaviors (methods).
Fields store the data or state of an object.
Methods define the actions or behaviors of an object.
Constructors are special methods used to initialize objects.
Access Modifiers control the visibility and accessibility of class members (fields, methods, constructors).