What is a Class?

Java classes are nothing more than templates or blueprints used to create objects. The main purpose of the class is to hold data/information or business logic.

In Java, you start writing code by creating Java classes. A class can have N data members called attributes and member functions that determine the behavior of the class.

A Java class can contain:

  1. Data members (attributes/properties)
  2. Member functions (methods)
  3. Constructors
  4. Nested class
  5. Main method (app start point)
  6. Business logic
  7. etc…

How to create a class?

We have been creating a Java class from our first tutorial now we will learn more about it from scratch. There are few rules and regulations we have to follow to create a Java class. Let’s start by looking at the syntax of the Java class.

Syntax

<modifier> class <Class-name> {
    
    // Class body

    /* 
     * All the data members and
     * member functions goes here
     */

}

Example

Following is the simplest example of a Java class ‘Person’ that holds first, last name and age of a person, and two member functions are their getFullName() and getAge() to access the data members.

public class Person {

    // Data members
    String firstName;
    String lastName;
    int age = 10;

    // A member function
    public String getFullName() {
        return firstName + " " + lastName;
    }

    // A member function
    public int getAge() {
        return age;
    }

}

Class naming conventions

  1. It is always good practice to write a Java class name in capitalize format.
  2. A Java class name should not match any reserved java keywords.
  3. The outermost class name and file name both should be the same, Like – Person.java
  4. A Java class name cannot start with a number.
  5. A Java class name cannot start with a special symbol other than ($ and _).
  6. Only two special symbols $ (dollar) and _ (underscore) are allowed in the class name.

A Java class can have optional access modifier (private, protected, public) but it is always a good practice to write required access modifier with a class.

Employee example

In the following example program, we are storing information about employees, we can create multiple objects of Employee class to store information of employees.

So far we have written everything in one Java class, but this time we follow OOP. So instead of putting everything in one class, we split it into two files.

  1. Employee.java – It will hold information about an employee.
  2. Main.java – A driver class containing the Java main method.

Download/Copy both the class and place them in a single directory and the run the Main class.

import java.util.Date;

// Employee class
public class Employee {

    String name;

    String address;

    Date dateOfJoin;

    int age;

    double salary;

    public void print() {
        System.out.println("Name: " + name);
        System.out.println("Address: " + address);
        System.out.println("Date of join: " + dateOfJoin);
        System.out.println("Age: " + age);
        System.out.println("Salary: " + salary);
    }
}
import java.util.Date;

// Main driver class
public class Main {

    public static void main(String[] args) {

        // Information of a employee
        Employee john = new Employee();

        john.name = "John";
        john.address = "US California";
        john.age = 20;

        john.dateOfJoin = new Date(); // Current date
        john.salary = 200000;

        // Print the data
        john.print();
    }
}

Compile

>> javac Main.java

You don’t need to compile both the Java classes separately. Compiling the Main class automatically compiles the Employee class and generates Main.class and Employee.class files.

Run

>> Main
Name: John
Address: US California
Date of join: Thu May 23 22:28:15 IST 2019
Age: 20
Salary: 200000.0