Java Class

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.

Example

public class Car {

int speed = 0;
int gear = 0;

void speedUp(int increment) {
    speed = speed + increment;
}

void changeGear(int newValue) {
    gear = newValue;
}

void applyBrakes(int decrement) {
    speed = speed - decrement;
}

void printStates() {
    System.out.println("speed=" + speed + " gear=" + gear);
}

}
 

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:

Example

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
        car1.changeGear(2);
        car1.speedUp(10);
        car1.printStates();


        car2.changeGear(4);
        car2.speedUp(20);
        car2.printStates();

        car3.changeGear(5);
        car3.speedUp(30);
        car3.printStates();
    }
}
 

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

Output
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 Single 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 single application:

Student.java

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}
 
School.java

 public class School {
    private String name;
    private Student student;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Student getStudent() {
        return student;
    }

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

}
 
SchoolExample.java

 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
    student1.setName("Danny");
    student1.setEmail("danny@tutorialsbuddy.com");

    student2.setName("Lenni");
    student2.setEmail("lenni@tutorialsbuddy.com");

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

    school2.setName("TutorialsBuddy School");
    school2.setStudent(student2);

    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());
}
}
 
Output
School 1:Harvard School, student name:Danny, student email:danny@tutorialsbuddy.com
School 2:TutorialsBuddy School, student name:Lenni, student email:lenni@tutorialsbuddy.com

Class Name 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.

Example
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 is Class needed in Java?

Java is an object-oriented programming language. Every code in Java, is associated with objects. Without a class it is not possible to create objects in Java.