Class in Java

A class is a blueprint or template from which individual objects are created. In the real world example, there may be thousands of other cars in existence, all of the same kind and model. Each car was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say your that your car is an instance of the class of objects known as cars.

The fields speed, and gear represent the object's state and the methods speedUp, changeGear, applyBrakes defines its interaction with the outside world.

You may have noticed that the Car class does not contain a main method. This is because it is not a complete application; the Car class is only the blueprint for cars that might be used in an application. The responsibility of creating and using the new Car object belongs to some other classes in your application.

Here is an example of a CarExample class that creates three separate Car objects and invokes their methods:

public class CarExample {

    public static void main(String args[]) {

        // Create three Car objects
        Car car1 = new Car();
        Car car2 = new Car();
        Car car3 = new Car();

        // Invoke method on the car objects



The output of this example prints the speed and gear for the three cars:

speed=10 gear=2
speed=20 gear=4
speed=30 gear=5

A class is always enclosed by an open and close curly braces. All code in Java is written within the open and close curly braces which is also called the body of a class. When creating a class, its name must always start with a capital letter, and the class name must always match the file name. All Java files have .java extension.

Creating Multiple Classes in a Java Program

A Java program may contain any number of classes. You should always create a separate class for different object type as it helps to keep the code clean and simple.

Here is an example of using multiple classes in a Java program:

public class Student {
    private String name;
    private String email;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getEmail() {
        return email;

    public void setEmail(String email) { = email;
public class School {
    private String name;
    private Student student;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public Student getStudent() {
        return student;

    public void setStudent(Student student) {
        this.student = student;

In the example code below, we will create objects from the above two classes and call their methods:

public class SchoolExample {

public static void main(String args[]) {

    // Two objects of Student class
    Student student1 = new Student();
    Student student2 = new Student();

    // Two objects of School class
    School school1 = new School();
    School school2 = new School();

    // putting student information on the student objects


    // putting school information on the school objects
    school1.setName("Harvard School");

    school2.setName("TutorialsBuddy School");

    System.out.println("School 1 details:" + school1.getName() + ", student name:"
            + student1.getName() + ", student email:" + student1.getEmail());
    System.out.println("School 2 details:" + school2.getName() + ", student name:"
            + student2.getName() + ", student email:" + student2.getEmail());

The output of the above code is as follows:

School 1:Harvard School, student name:Danny, student
School 2:TutorialsBuddy School, student name:Lenni, student

Class Naming Convention

You can create a class with any name. But it is always better to follow a good naming convention while writting a program for real world applications. As discussed earlier, your class name must be always start with a capital letter and not with a number or any special character. Start the first letter of every word with a Capital letter if your class name has multiple words.

Valid Class Names
Invalid Class Names
Student, Customer, Doctor, School, Instructor, OldUser, NewUser
2Student, _Customer, $Doctor, @School, !Instuctor, ~OldUser, [NewUser

Class Members

A Java class may contain:

  • Constuctors
  • Variables
  • Methods
  • Nested Classes

Why are Classes needed in Java?

Classes are needed in Java because they provide the foundation for creating objects, encapsulating data and behavior, promoting code reusability, supporting inheritance and polymorphism, and enabling the object-oriented programming paradigm in Java.