Class skeleton
- Java course
- Beginner
- Lesson 4/116
- 4 min
Learn the public class and static method structure.
What is public class?
In Java every source file must contain at least one class. A class is declared with the class keyword and must share the file name: a Main.java file holds public class Main.
The public modifier marks the class as accessible from outside. The class body sits between curly braces { } and contains methods and variables.
public class Main {
// methods here
}Only one public class may live in a file; other classes can be package-private.
The main method skeleton
The main method sits inside the class and is the entry point of a Java program. Its signature is always the same:
public class Main {
public static void main(String[] args) {
System.out.println("Hello");
}
}Four parts work together: public class Main is the shell, public static void main is the entry point, String[] args holds command-line parameters, and System.out.println is the output.
This skeleton is the basis of almost every Java program.
Practice Complete the class skeleton
Define a Main class. Add a main method inside. From main, print Java 21 with println.
Starter code
public class Main {
// Add main method and println
}Sample solution
public class Main {
public static void main(String[] args) {
System.out.println("Java 21");
}
}Expected output
Java 21
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Complete the class declaration
public ____ Main { }
public class Main { }
Explanation: public class Main declares a class.
2 Match the keywords
| Term | Meaning |
|---|---|
public |
Accessible |
class |
Class declaration |
static |
Class-level |
void |
No return |
3 What is the class naming convention?
- kebab-case (main-app)
- snake_case (main_app)
- PascalCase (MainApp) Correct answer
- ALL_CAPS (MAIN_APP)
Explanation: Java classes use PascalCase.