In Java, a class is a blueprint or template for creating objects. It defines a type by bundling data and methods that work on the data into a single unit. A class can contain fields (variables), methods, constructors, blocks, and nested classes and interfaces.
Declaring a Class in Java
To declare a class in Java, you use the class
keyword followed by the class name. The body of the class is enclosed in curly braces {}
. Here’s a basic structure of a class declaration:
public class ClassName {
// Fields (variables)
int field1;
String field2;
// Constructor
public ClassName(int field1, String field2) {
this.field1 = field1;
this.field2 = field2;
}
// Methods
public void displayInfo() {
System.out.println("Field1: " + field1 + ", Field2: " + field2);
}
// Other methods, blocks, nested classes/interfaces can be added here
}
Components of a Class
- Fields: Variables that store the state or data of an object.
- Constructor: A special method used to initialize objects. It has the same name as the class and no return type.
- Methods: Functions defined inside a class that describe the behaviors or actions an object can perform.
- Blocks: Code blocks, like static blocks or instance initializer blocks.
- Nested Classes/Interfaces: Classes or interfaces defined within another class.
Example: Declaring a Class in Java
Here’s an example of a class Car
that includes fields, a constructor, and methods:
public class Car {
// Fields (variables)
private String color;
private String model;
// Constructor
public Car(String color, String model) {
this.color = color;
this.model = model;
}
// Methods
public void displayInfo() {
System.out.println("Car model: " + model + ", Color: " + color);
}
// Getter and Setter methods
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
Creating Objects from a Class
To use the class, you create objects (instances) of the class. Here’s how you can create objects and call methods on them:
public class Main {
public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car("Red", "Toyota");
// Calling methods on the object
myCar.displayInfo(); // Output: Car model: Toyota, Color: Red
// Using getter and setter methods
myCar.setColor("Blue");
System.out.println("Updated Color: " + myCar.getColor()); // Output: Updated Color: Blue
}
}
Access Modifiers
Classes and their members (fields, methods, etc.) can have access modifiers to define their visibility:
public
: The class or 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 same package and subclasses.- Default (no modifier): The member is accessible within the same package.
Summary
A class in Java is a fundamental building block used to create objects and define their properties and behaviors. By using classes, Java promotes the principles of object-oriented programming, making it easier to manage and organize complex software systems.