Java
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Java
-
Section 4: Collections Framework
-
Section 5: Exception Handling
-
Section 6: Generics
-
Section 7: Functional Java
-
Section 8: Concurrency
-
Section 9: I/O and NIO
-
Section 10: JVM Internals
-
Section 11: Modern Java Features
-
Section 12: Build Tools and Project Structure
-
Section 13: Testing
-
Section 14: Databases and Persistence
-
Section 15: Networking
-
Section 16: Design and Best Practices
-
Section 17: Reflection and Annotations
-
Section 18: Logging and Diagnostics
-
Section 19: Date, Time, and Internationalization
-
Section 20: Java Platform Module System
-
Section 21: Security in Java
-
Section 22: Advanced Collections and Data Structures
-
Section 23: More Concurrency Patterns
-
Section 24: Compression, Files, and System Integration
-
Section 25: GUI Programming
-
Section 26: Practical Projects
-
Section 27: Data Structures and Algorithms
-
Section 28: Interview and Algorithm Practice
-
Section 29: JSON and Data Interchange
-
Section 30: More Concurrency Utilities
-
Section 31: More Collections and Streams Practice
-
Section 32: More File and System Programming
-
Section 33: Standard Library Deep Dive
-
Section 34: More Practice and Drills
-
Section 35: More Testing and Quality
-
Section 36: More Design Patterns and Architecture
-
Section 37: Career and Ecosystem
-
Section 38: More OOP and Architecture Practice
-
Section 39: More Enterprise Concepts
-
Section 40: Advanced JavaFX
-
Section 41: More Interview Practice
3: Anatomy of a Java Program
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);
}
}There are no comments for now.