Classes in Java

A class is a blueprint or template that defines the properties and behaviors of objects. It is a fundamental building block in object-oriented programming (OOP), allowing developers to model real-world entities and simulate their interactions. In simple terms, a class is like a plan or a design that describes how something should look and behave. This something can be anything you want to represent in a computer program, like a car, a person, or a game character.

Imagine you want to create a game with different characters. Instead of creating each character separately, you can have a class called Character that defines how all characters should be. It's like a blueprint that says, all characters will have a name, health, and the ability to move.

So, a class is a way to organize and structure your code. It helps you create objects (like game characters) with consistent properties (like health and name) and actions (like moving).

Class Declaration

In Java, a class is declared using the class keyword. The basic syntax of a class is as follows:

public class MyClass {
    // Fields (variables)
    // Constructors
    // Methods
}

Here, public is the access modifier, indicating that the class is accessible from other classes. MyClass is the name of the class; you can replace this name with your desired class name.

Class Members

A Java class may contain:

  1. Fields (Variables):
  2. A class can have fields, which are variables that represent the attributes or properties of objects. Fields define the state of the objects created from the class. For example:

    public class Car {
        String make;
        String model;
        int year;
    }
  3. Methods:
  4. Methods represent the behavior of objects. In simple terms, methods are like the abilities or actions that objects can perform. Think of them as the things an object knows how to do. They are special functions associated with a class. For example, if you have a class called Car, a method for that class could be startEngine() because starting the engine is something cars can do. Another method might be stopEngine() because driving is another action that cars can perform. For example:

    public class Car {
        String make;
        String model;
        int year;
    
        // Method
        public void startEngine() {
            System.out.println("The car is starting.");
        }
    
        // Method
        public void stopEngine() {
            System.out.println("The car is stoping.");
        }
    }
  5. Nested Classes:
  6. Nested classes are classes defined within another class. For example:

    public class OuterClass {
        // Outer class members
    
        // Nested class
        public class InnerClass {
            // Inner class members
        }
    }

You might have observed that the Car class doesn't include the main method. This is because it's not a complete application by itself. The Car class serves as a blueprint for cars that could be used in an application. The task of creating and utilizing a new Car object can be handled by other classes within the application.

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.

Class Naming Convention

You can create a class with any name, but it is always better to follow a good naming convention when writing a program for real-world applications. As discussed earlier, your class name must always start with a capital letter and not with a number or any special character. If your class name has multiple words, start the first letter of every word with a capital letter.

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

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.