Skip to content

Anatomy of a Java Program

Let’s go through the anatomy of a Java program

A function is the smallest building block used to perform tasks in a Java program. Every Java program requires at least one function, which serves as the entry point to the application. This entry point is always the main function.

Example function code block:

ReturnType Name(parameters) {
...
}
  • The ReturnType specifies the type of value the function will return. If no value is expected, the return type is void.
  • A function cannot exist independently and must be defined within a class.

A class is a container that can hold one or more related functions (or methods). Every Java program requires at least one class that contains the main entry point to the program. The name of this class is commonly Main, but this can vary.

Example of a class with the main function:

class Main {
void main() {
...
}
}

In Java, the functions defined within a class are called methods.

Access modifiers are special keywords used to define the visibility or accessibility of classes and their members (methods, variables, etc.). Common access modifiers include:

  • public: Accessible by any other class.
  • private: Accessible only within the same class.

Example with an access modifier:

public class Main {
public void main() {
...
}
}
  • In Java, class names follow the PascalCase convention, where the first letter of each word is capitalised (e.g., Main, CustomerService).
  • Method names follow the camelCase convention, where the first letter is lowercase, and the first letter of subsequent words is capitalised (e.g., calculateInterest(), printReceipt()).