Java Basics: Writing your first program
Cal Afun

Cal Afun @cal_afun_2475adb4b562023b

About: Hi, I’m Cal, a Computer Engineering student passionate about software development and learning by teaching. My goal is to break down concepts in a simple, beginner-friendly way.

Joined:
Aug 17, 2025

Java Basics: Writing your first program

Publish Date: Aug 18
6 0

Hello guys 👋 Today we’re kicking off the first session in my Java learning series.
We’re starting with the most basic form of a Java program.
It’s important that as beginners, we don’t overlook simple examples as they may look small, but they build the foundation we need to understand bigger and more complex programs later on.

📝 The Program

Here’s the simplest Java program we’ll be looking at:

public class FirstSample {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Enter fullscreen mode Exit fullscreen mode

Keywords You Should Understand Before Continuing

  1. JVM (Java Virtual Machine): The JVM is like the heart of Java. When you write Java code, it doesn’t run directly on your computer’s hardware. Instead, it’s compiled into bytecode (a universal form of code). The JVM’s job is to read and execute this bytecode on any machine. 👉 This is why Java is often called “write once, run anywhere.”
  2. Class: In Java, everything must live inside a class. You can think of a class as a container that groups related code together. 👉 Without a class, your program won’t even run.
  3. Method: A method is like a function in other languages, but in Java, it always belongs to a class. Methods define what actions a class can perform. 👉 The most important method for now is main, because it’s always the starting point of a Java program.
  4. Static: Normally, you need to create an object (instance) of a class before using its methods. But if a method is marked as static, you can use it without creating an object. 👉 That’s why main is static — so the JVM can run it without needing to create an object first.
  5. void: The void keyword simply means “this method does not return anything.” For example, main just executes code; it doesn’t give back a result.
  6. String[] args: This represents an array of strings passed into the program from the command line. 👉 For now, just know that it can capture extra information when running your program.
  7. IDE (Integrated Development Environment): An IDE is basically a smart workspace where you write, edit, run, and debug your code. Instead of using a simple text editor, an IDE gives you helpful tools like syntax highlighting, auto-completion, debugging tools, and one-click run buttons. 👉 Popular IDEs for Java are IntelliJ IDEA, Eclipse, and NetBeans. Beginners often start with IntelliJ IDEA Community Edition or even VS Code with the Java extensions.
  8. Compiler (javac): Java is a compiled language. This means before the code can run, it needs to be translated into bytecode that the JVM understands. 👉 The compiler (javac) does this translation step — it takes your .java file and produces a .class file containing bytecode.

Understanding the Syntax
The first thing you'd need to know about java is that java is case sensitive. Writing Main instead of main would lead to an error. So make sure you take note of all the correct format to be used.

Now we are going to decipher the source code line by line to understand exactly what we are doing.

  1. public class FirstSample: The first keyword in this line is public and it is called an access modifier. Now it's function is to determine who has access or better still who can use or see the specific class. Hence by making the access modifier public we are saying that the class is visible to everyone inside and outside the program

The second keyword class is very important as every single java code must be wrapped in a container (the technical term is class) which holds the program's logic. If you are already familiar with python then you would know that you could easily begin to type a code like print('Hello World') to get the Hello World printed on the terminal. For java, the print statement needs to be placed inside a class otherwise it won't run. This is why we are wrapping everything in a class.

The next word, which is not a keyword is FirstSample which is basically the name of the class. Just like you won't give birth and leave the child nameless you can't create a class and give it no name. For java the file name must match the class name. Hence in our scenario the file name is going to be FirstClass.java(remember it is case sensitive so firstclass won't work!). In Java there is also a very specific way we name our classes. The class name must begin with a capital letter and if there's any other word it will also be capitalised with no space between the words i.e FirstClass and not First Class.

Lastly, if you noticed the whole statement was wrapped in curly bracket ({}). This is what wraps and concludes our block of code so it is important you don't forget it otherwise you are going to come across very scary error messages in your terminal 😅.

I usually like to understand my code like it is English, so if I were to translate the first line(public class FirstSample) to English, all I'd be saying is:“Hey JVM, this is my class, I’m making it public so you (and anyone else) can access it, and its name is FirstSample.”

  1. The second line of code is creating a method (a function within a class). Just like the creation of the class itself, we give it the access modifier public to enable the JVM to have access to the method to enable us run it. This method is special because it’s called main, and it’s always the entry point of any Java application. For now as beginners let's take it as though you can't write a java program without the main method

If you already have some experience with OOP, then you would come to realise that normally you would have to create an instance of a class before you could access any method for that class, but by using the keyword static, we are telling the JVM to bypass creating an instance of the class before running the code. This is because the main method is like a static method, it does not need an instance to be created for it to work. The void keyword is basically telling the JVM that the method does not return anything. In other words, after executing the code inside it, the JVM won’t expect any result back.

public static void main(String[] args). Taking a closer look at this code, you'll get to know that String[] args have been passed in as a parameter to the method. The string keyword is basically letting Java know that the parameter is to be of the string type and the square brackets also indicate that it is an array. The word args is just a convention, but could be replaced with a variable name which represents the array.
So in English the the line String[] args represents an array of Strings called args that stores command-line arguments. Remember that because it is a method, we close it up with the curly brackets.

So in english the line of code, public static void main(String[] args), means "This is a method called main that anyone can use, it doesn’t need an object to run, it doesn’t give back a result, and it accepts a list (array) of text values called args as input."

  1. Now to print something onto the terminal we will use the built-in java object System.out which has methods print and println, among others. We use the the println to print onto a line a specific text, in this case Hello World.

Once your code is written and saved as FirstSample.java, you can run it in two ways:
Using the terminal:
First compile:
javac FirstSample.java
This creates a FirstSample.class file .
Then run:
java FirstSample
Using an IDE (like IntelliJ, Eclipse, or VS Code):
Simply click the Run button, and the IDE will handle the compile and run steps for you.

Congratulations you are now eligible to write your own Java Program. Here are some task to guide you;

  1. Hello, [Your Name]
    Modify the Hello World program so that it prints your name instead.

  2. Favorite Quote
    Write a program that prints your favorite quote on the terminal using System.out.println.

  3. Multiple Prints
    Print three different lines of text (e.g., your name, favorite food, favorite hobby). Try using both print and println and observe the difference.

  4. Experiment with Class Name
    Change the class name from FirstSample to something else (e.g., MyFirstJava) and make sure the file name matches the class name. Run it successfully.

📚 Things to Do If You Didn’t Fully Understand This Blog

Don’t worry if some concepts still feel confusing. That’s normal when starting out with Java. Here are a few things you can do to deepen your understanding:
🎥 Watch YouTube Tutorials
Search for beginner-friendly videos on Java classes and the main method. Sometimes hearing an explanation in a different way helps things click.
📖 Read Textbooks
I recommend Core Java by Cay S. Horstmann and Gary Cornell. It’s one of the best books for beginners, and it goes into detail without overwhelming you.
🌐 Check Online Resources
Explore Java documentation and tutorials on sites like GeeksforGeeks, W3Schools, or Oracle’s official Java tutorials.
✍️ Practice Writing Code
Re-type the examples instead of just copy-pasting. You’ll understand things better by actually writing them out.
💬 Ask Questions
Don’t hesitate to ask me (or your peers) if you’re still confused. Sometimes a short explanation clears up hours of confusion.

Comments 0 total

    Add comment