Java Objects and Classes Class Creation

Learning objective: By the end of this lesson, students will be able to create classes that comply with Java’s rules for classes.

Classes in Java

A class is a blueprint for creating objects. It defines the object’s properties (fields) and behaviors (methods).

Rules for creating classes

  1. Classes are created in .java files.
  2. The first line of the .java file is the package statement. It represents the package to which the classes in the file belong. For projects managed by Maven, a package statement is required to map the class to the appropriate directory structure.
    • Example: package org.example;
  3. A .java file can contain only one public class. If a public class is present, the file name must match the name of the public class, including capitalization.
    • For example, here is a Main.java file:

      package org.example;
           
      public class Main {
          public static void main(String[] args) {
          Helper helper = new Helper();
          helper.greet();
          }
      }
           
      class Helper {
          void greet() {
              System.out.println("Hello from Helper!");
          }
      }
      
  4. Other classes in the same .java file must not be declared as public. They will have the default (package-private) access level.
  5. If no class in the file is declared public, the file name can be any name.
  6. For the class with the main method, the file name and the public class name must match to ensure the JVM can locate the entry point.
  7. The classes in different packages can interact with one another, but access depends on the access modifiers (public, protected, default).
  8. Class names must begin with a capital letter and capitalize the first letter of each word they include. Consequentially, the .java file name also must use the same convention as it is case sensitive.

Creating classes inside Packages

In IntelliJ IDEA IDE, we can follow the below steps to create a class inside a package:

  1. Right-click the desired package.
  2. Select New > Java Class.
  3. Enter an appropriate class name.
    • For example, creating a Student class inside the com.school package.
  4. Declare and define the required properties and methods.
    • For example:
     package com.school;
    
     public class Student {
         public String name;
         public int grade;
    
         public void displayInfo() {
             System.out.println("Name: " + name + ", Grade: " + grade);
         }
     }
    

Things to be mindful of

While declaring properties and methods, we need to be mindful of when to use:

Instance variables vs static variables

Aspect Instance variables Static variables
Declaration Declared without static keyword. Declared using the static keyword
Storage Each object of the class has its own copy of instance variables. Only one copy exists, shared among all objects instantiated from the class. Modification in one object reflects the change in all other objects.
Access qualifier Only the object’s name can be used as an access qualifier. The object or class name can be used as an access qualifier.
Lifespan Exists only as long as the object exists. Exists for the entire duration of the program’s runtime.

Instance methods vs static methods

Aspect Instance methods Static methods
Declaration Declared without static keyword. Declared using the static keyword.
Can access Only instance variables and methods. Do not act on instance variables.
Access qualifier Only object name can be used as an access qualifier. Object name or class name can be used as an access qualifier.

Demo

class Library {
    // Instance variable
    String bookName;

    // Static variable
    static int totalBooks = 0;

    // Constructor
    Library(String bookName) {
        this.bookName = bookName;
        totalBooks++;
    }

    // Instance method
    void displayBook() {
        System.out.println("Book Name: " + bookName);
    }

    // Static method
    static void displayTotalBooks() {
        System.out.println("Total Books: " + totalBooks);
    }
}

public class Main {
    public static void main(String[] args) {
        Library book1 = new Library("Java Basics");
        Library book2 = new Library("Advanced Java");

        // Instance method usage
        book1.displayBook(); // Output: Book Name: Java Basics
        book2.displayBook(); // Output: Book Name: Advanced Java

        // Static method usage
        Library.displayTotalBooks(); // Output: Total Books: 2
    }
}