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
- Classes are created in
.javafiles. - The first line of the
.javafile 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;
- Example:
- A
.javafile can contain only onepublicclass. If apublicclass is present, the file name must match the name of thepublicclass, including capitalization.-
For example, here is a
Main.javafile: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!"); } }
-
- Other classes in the same
.javafile must not be declared aspublic. They will have the default (package-private) access level. - If no class in the file is declared
public, the file name can be any name. - For the class with the
mainmethod, the file name and thepublicclass name must match to ensure the JVM can locate the entry point. - The classes in different packages can interact with one another, but access depends on the access modifiers (
public,protected, default). - Class names must begin with a capital letter and capitalize the first letter of each word they include. Consequentially, the
.javafile 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:
- Right-click the desired package.
- Select New > Java Class.
- Enter an appropriate class name.
- For example, creating a
Studentclass inside thecom.schoolpackage.
- For example, creating a
- 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
}
}