A class is one of the fundamental concepts of Java. It defines the properties and behaviors that objects created from that class can have.
A class is a blueprint or template used to create objects. It can contain variables, constructors, and methods.
For example, a Student class can contain:
class Student {
String name;
int age;
void display() {
System.out.println(name);
System.out.println(age);
}
}
The general syntax of a class is:
class ClassName {
// Fields or variables
// Constructors
// Methods
}
Example:
class Car {
String color;
int speed;
void start() {
System.out.println("Car is starting");
}
}
Here, Car is the class name,
color and speed are fields,
and start() is a method.
An object is an instance of a class. We commonly
create an object using the new keyword.
class Student {
String name = "Rahul";
void display() {
System.out.println(name);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.display();
}
}
Rahul
Explanation:
Student is the class.s1 is a reference variable.new Student() creates an object.s1.display() calls the method.Variables declared inside a class but outside its methods are called fields or member variables.
class Employee {
String name = "Amit";
int salary = 25000;
public static void main(String[] args) {
Employee e1 = new Employee();
System.out.println(e1.name);
System.out.println(e1.salary);
}
}
Amit
25000
Non-static fields belong to individual objects. Each object can have its own field values.
A method defines an action or behavior that an object can perform.
class Calculator {
int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
Calculator c1 = new Calculator();
int result = c1.add(10, 20);
System.out.println(result);
}
}
30
The add() method accepts two integers
and returns their sum.
We can create multiple objects from the same class. Each object can store different instance field values.
class Student {
String name;
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
s1.name = "Rahul";
s2.name = "Priya";
System.out.println(s1.name);
System.out.println(s2.name);
}
}
Rahul
Priya
Java classes can contain both static and instance members.
| Static Member | Instance Member |
|---|---|
| Associated with the class. | Associated with each object. |
| Accessed using the class name when accessible. | Usually accessed through an object. |
| One class-level field is shared. | Each object has its own instance field. |
class Demo {
static String college = "ABC College";
String studentName;
public static void main(String[] args) {
Demo d1 = new Demo();
Demo d2 = new Demo();
d1.studentName = "Rahul";
d2.studentName = "Priya";
System.out.println(Demo.college);
System.out.println(d1.studentName);
System.out.println(d2.studentName);
}
}
ABC College
Rahul
Priya
Members of a class can be accessed according to their access modifiers and whether they are static or instance members.
class Person {
String name = "Amit";
void introduce() {
System.out.println("My name is " + name);
}
public static void main(String[] args) {
Person p1 = new Person();
System.out.println(p1.name);
p1.introduce();
}
}
Amit
My name is Amit
The dot operator (.) is used to access
an object's accessible fields and methods.
Consider this simple example:
class Mobile {
String brand;
int price;
}
Mobile is a class. We can create objects
representing different mobile phones.
Mobile m1 = new Mobile();
m1.brand = "Samsung";
m1.price = 20000;
Mobile m2 = new Mobile();
m2.brand = "Apple";
m2.price = 50000;
| Object | Brand | Price |
|---|---|---|
| m1 | Samsung | 20000 |
| m2 | Apple | 50000 |
Access modifiers control where classes and their members can be accessed.
| Modifier | Meaning |
|---|---|
public |
Accessible wherever Java access rules permit. |
private |
Accessible within the declaring top-level class. |
protected |
Accessible in the same package and in qualifying subclasses. |
| No modifier | Package-private access. |
A top-level class can generally be declared
public or package-private, not
private or protected.
A constructor initializes a newly created object. It has the same name as the class and has no return type.
class Student {
String name;
int age;
Student(String studentName, int studentAge) {
name = studentName;
age = studentAge;
}
void display() {
System.out.println(name);
System.out.println(age);
}
public static void main(String[] args) {
Student s1 = new Student("Rahul", 20);
s1.display();
}
}
Rahul
20
The constructor receives values when the object is created and assigns them to the instance fields.
class BankAccount {
String accountHolder;
double balance;
BankAccount(String name, double initialBalance) {
accountHolder = name;
balance = initialBalance;
}
void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
void displayBalance() {
System.out.println("Account Holder: " + accountHolder);
System.out.println("Balance: " + balance);
}
public static void main(String[] args) {
BankAccount account =
new BankAccount("Rahul", 1000);
account.deposit(500);
account.displayBalance();
}
}
Account Holder: Rahul
Balance: 1500.0
This example combines fields, a constructor, methods, and an object in one class.
Student class with name, age,
and a method to display student details.
Employee class with name,
salary, and department.
Car class with brand, color,
and a method named start().
Mobile class
and assign different brand names and prices.
Rectangle class with length
and width fields and a method to calculate area.
Book class with a constructor
that initializes the title and author.
BankAccount class with
deposit and balance-display methods.
1. What is a class in Java?
2. Which keyword is commonly used to create an object?
3. What is a constructor used for?