JAVA FOUNDATIONS · JAVA 17
Java program structure: from source file to output
Run a complete Java program, trace main and a named method, and separate compile errors from incorrect results.
Forsk Coding School learning resources · Updated 2026-10-26
What you will learn
- Compile and run Main.java
- Explain the entry point and a method call
- Predict output before executing code
Before you start: A JDK with javac and java available in your terminal. Examples use standard Java 17 without preview features.
Start with one file and one observable result
A first program is easier to understand when it has a clear input, a short calculation and visible output. In this example the input is two book prices written directly in the source. The calculation adds them. The output is a two-line receipt. You do not need a framework, build tool or an account to follow the calculation.
Create a plain text file named Main.java. The public class in the example is Main, so the filename must match that name, including its case. Open a terminal in the file's directory, run javac Main.java, then run java Main. The compiler checks the source and produces a class file. The java command starts the compiled class; do not add .class to its name.
Read the program as a sequence of calls
The conventional entry point here is public static void main(String[] args). Execution begins inside its braces. The variable total is calculated before printReceipt(total) runs. Calling the method transfers execution to that method's body; after it finishes, execution returns to the next statement in main. Declaration order does not mean every method runs automatically.
A semicolon ends a statement such as a variable assignment or a method call. Braces group a class or method body. Indentation makes those groups visible to a reader, but the braces determine the actual boundaries. Keep one statement per line while learning so a compiler error points near one small piece of work.
Build a repeatable debugging routine
If javac fails, read the first reported error before the later messages. A missing quote or brace can produce several follow-on errors. Correct one cause, compile again and only then run the program. If compilation succeeds but the total is wrong, trace the values instead: a compiler can accept a calculation that does not match your intention.
Check java -version and javac -version when a tutorial behaves differently on two computers. This lesson deliberately uses a traditional class and entry point so it runs on the stated Java 17 baseline. A newer IDE may offer different source-file styles; using the same structure as the example keeps the first debugging session focused.
Run the worked example
Save as Main.java. Run javac Main.java, then java Main.
public class Main {
static void printReceipt(int total) {
System.out.println("Book total");
System.out.println(total);
}
public static void main(String[] args) {
int firstPrice = 120;
int secondPrice = 80;
int total = firstPrice + secondPrice;
printReceipt(total);
}
}Expected result
Book total
200- main assigns 120 and 80, then adds them to obtain 200.
- printReceipt receives the integer value as its total parameter.
- println writes each value and then moves to a new line.
Common mistakes to check
- Main.java and public class Main must agree in spelling and case.
- A named method runs when called; placing it above main does not execute it first.
- After changing source, compile again so java does not run an older class file.
Try it yourself
Write a method named showWelcome that prints Learning Java, then 3. Call it exactly once from main. Predict the number of output lines before running it.
Give me a hint
Use a void method with no parameters and put two println statements inside its braces.
Compare with one working solution
public class Main {
static void showWelcome() {
System.out.println("Learning Java");
System.out.println(3);
}
public static void main(String[] args) {
showWelcome();
}
}Expected result
Learning Java
3Quick check
Choose an answer, then check the explanation. Your answer stays in this page and is not saved as a student record.
Keep learning
Explain the result in your own words, change one input and predict the new output. For additional structured assignments, explore the workbook sample before requesting the full pack.
Explore the practice workbookReference documentation
Use the official reference to check language details. The explanations, examples and exercises on this page are written for this learning resource.
