hola-mundo-java-estructura-main

Your First Java Program: Structure, Main, and Compilation

  • 4 min

A Hello World is a minimal program that displays a message on the screen. It is the traditional example to verify that the language and its tools are correctly installed.

We will not just copy and paste. We are going to dissect the classic structure of a Java program and run it from the console, to understand what happens when you press the run button in the IDE.

The Code: HolaMundo.java

Open any plain text editor (Notepad, VS Code without extensions…) and write this. Respect uppercase and lowercase letters, Java is very strict about that.

public class HolaMundo {
    public static void main(String[] args) {
        System.out.println("Hola Mundo desde Java");
    }
}
Copied!

Save the file exactly with the name HolaMundo.java.

When a top-level class is public, the file name must match exactly the class name. If the class is HolaMundo, the file must be HolaMundo.java.

Code Anatomy

Before running it, let’s understand what we just wrote. Java is known for being “verbose” (many words). Each one has a reason for existing.

public class HolaMundo

In the traditional Java structure, code is declared inside classes. Java 25 also allows compact source files for small programs, but we’ll start with the classic form because it is what you will find in real-world projects.

  • class: Defines that we are going to create a class.
  • HolaMundo: This is the identifier (name) of the class. By convention, we use PascalCase (first letter capitalized).

public static void main(String[] args)

This is the classic entry point of the application. Java 25 supports other simplified forms of main, although this one remains the most recognizable and compatible.

  • public: The method must be accessible from “outside” (by the JVM).
  • static: Key concept. It means this method belongs to the class and not to a specific object. The JVM can call it without having to create an instance (new HolaMundo()) first.
  • void: It does not return any value. (Unlike C++, where main usually returns an int to indicate the exit status).
  • main: The reserved name that the JVM looks for.
  • String[] args: This is an Array of text strings. This is where the arguments you type in the console when launching the program are received.

System.out.println(...)

This is the standard instruction for printing to the console.

  • System: A system class.
  • out: A static object inside System that represents the standard output (your screen).
  • println: “Print Line” method (prints and jumps to a new line).

Manual Compilation and Execution

Now we are going to do what the IDE does behind the scenes. Open your terminal (CMD or PowerShell) and navigate to the folder where you saved the file.

Compile with javac

The computer does not understand your .java file. We need to translate it into Bytecode. For this, we use the javac compiler.

javac HolaMundo.java
Copied!

If everything went well… nothing will happen. In the Unix/Linux philosophy, “no news is good news”. If there are no errors, the cursor will simply jump to the next line.

Now look in your folder. A new file will have appeared: HolaMundo.class. This file contains the bytecode that a compatible JVM can run on Windows, Linux, or macOS, as long as the program does not depend on platform-specific native resources.

Run with java

Now we need to start the JVM so it can read that Bytecode. We use the java command (the launcher).

java HolaMundo
Copied!

Be careful! Notice that I wrote java HolaMundo and NOT java HolaMundo.class. When executing, we specify the class name, not the file name. If you include the extension, you will get an error.

If you did it correctly, you will see on your screen: Hola Mundo desde Java

The Program Lifecycle

The complete process can be summarized as follows:

Editing: You create Archivo.java (Human-readable).

Compilation: javac Archivo.java -> Generates Archivo.class (Bytecode).

Loading: The JVM reads the .class and loads it into memory.

Verification: The JVM checks that the Bytecode is safe and valid.

Execution: The JVM translates to machine code and your CPU executes the instructions.