Skip to Content
Course content

3: Anatomy of a Java Program

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I want to start today by showing you a snippet of code that a junior dev sent me a few years ago. He was trying to build a simple tool to calculate the area of a circle, but he was pulling his hair out because the compiler was throwing a wall of red text at him. Here is exactly what he wrote:

public class CircleArea {
    double radius = 5.0;
    System.out.println("Calculating area for radius: " + radius);

    public void main(String args) {
        double area = Math.PI * radius * radius;
        System.out.println("The area is: " + area);
    }
}

At first glance, it looks logical. He defined a radius, printed it, and then tried to do the math in a main method. But this code won't even compile. If you tried to run this, you'd get an error saying something like expected and another telling you that the main method isn't being recognized as the entry point of the program.

The "Code in No-Man's Land" Problem

The first big issue is that System.out.println is sitting directly inside the class body. In Java, you can't just have "loose" logic floating around inside a class. A class is a blueprint; it can hold variables (fields) and methods, but it cannot execute a command like "print this to the console" unless that command is wrapped inside a method.

I like to think of the class as a building and the methods as the rooms. You can put furniture (variables) in the hallways, but you can't actually "live" or "work" (execute logic) until you step inside a room. To fix this, that print statement needs to be moved inside the main method.

The Missing Static and the Array Signature

Even after moving the print statement, this program still won't run. Look closely at the method signature: public void main(String args). This is a classic "almost right" mistake.

The Java Virtual Machine (JVM) is incredibly picky. It doesn't just look for a method named "main"; it looks for a very specific signature: public static void main(String[] args). Two things are missing here: the static keyword and the square brackets [] after String.

  • static: This tells Java that the method belongs to the class itself, not to a specific object created from the class. Since the program hasn't started yet, there is no "object" to call a method on, so the entry point must be static.
  • String[] args: The JVM expects an array of strings, not a single string. Even if you don't plan on passing any arguments into your program from the command line, the signature must include those brackets, or Java will simply ignore the method and tell you it can't find the main method.

Here is the corrected version:

public class CircleArea {
    public static void main(String[] args) {
        double radius = 5.0;
        System.out.println("Calculating area for radius: " + radius);
        
        double area = Math.PI * radius * radius;
        System.out.println("The area is: " + area);
    }
}

Matching Class Names to File Names

One last thing that often trips people up: the relationship between the code and the disk. In Java, if you have a public class CircleArea, the file must be named CircleArea.java. Exactly. Case-sensitive.

If you name the file circlearea.java or Main.java, the compiler will throw a fit. I've spent way too many hours of my life debugging "missing" classes only to realize I had a lowercase letter where a capital one should be. It's a rigid rule, but it helps the JVM find the entry point of your application across thousands of files in a large project.

The Hierarchy of a Java File

To wrap your head around the anatomy, just remember the nesting order. It always flows from the general to the specific:

  • Package Declaration: (Optional) At the very top, telling Java which folder this file belongs to.
  • Imports: (Optional) Bringing in tools from other libraries (like java.util.Scanner).
  • The Class: The outer wrapper. Everything in your program must be inside a class.
  • Methods: The logic blocks inside the class.
  • Statements: The individual lines of code inside the methods.



📋 Practical Task

Fixing the Broken ATM Simulator

Below is a piece of code for a simple ATM balance checker. It contains three structural errors based on what we just covered: a naming mismatch, a signature error in the main method, and logic placed outside of a method.

Your Task: Rewrite the code so that it compiles and runs correctly. Assume the file is named AtmSimulator.java.

public class AtmSim {
    double balance = 1000.00;
    System.out.println("Welcome to the Java Bank!");

    public void main(String args) {
        System.out.println("Your current balance is: $" + balance);
    }
}
Rating
0 0

There are no comments for now.

to be the first to leave a comment.