Skip to content

Simple Hello World

In this post, I would like to show you how to create a simple Java class with this pure hands-on tutorial and help to understand some basic concepts, let’s go for it!

I hope you have checked my previous post: how to install your IDE and now you have it up and running!

Here you have a video of the MyItBasics Simple Hello World

Create a new workspace directory

The first step in your development setup is to define and create your IDE workspace. This would be a local directory where your projects will be created.

When you start your IDE (Eclipse, RHJDS, or similar) a window will ask you for the workspace to use. In others like IntelliJ, it will use a default workspace but you can change it after the IDE is running.

As we are using RHJDS the image below is a sample of how the workspace dialog looks like. Here you can also create a new workspace directory if it does not exist physically.

In order to do so, just type the new directory path and it will create the new folder in your filesystem.

How to create a simple Java class tutorial?

Now it is time to create your first project. As you will see there are different types of projects and we will use some of them in this blog. However, we are interested in a new Java project.

Here are the steps:

  1. In the main menu select File -> New -> Project
  2. ALTERNATIVE
    1. Right-click on the Project Explorer view.
    2. Select New -> Project

  3. In the New Project dialog open the Java folder and select Java Project
  4. A new dialog to configure the Java project appears and allows you to define some initial settings for your project
    1. Project name: MySimpleHelloWorld
    2. JRE: Java 8+

  5. A new dialog will request to open a new perspective associated with the project. I usually recommend this option due to the additional default views that comes handy to work with such projects
  6. And finally you will see the project in your Package Explorer view
  7. Create new package
    1. Right-click on the src folder and select New -> Package
    2. On the package dialog set the name: main.java.com.myitbasics.initial

Create a new Java class

  1. Right-click on the new package main.java.com.myitbasics.initial and select New -> Class
  2. In the class dialog:- Name: SimpleHelloWorld- Check the stub for the public static void main method

Write your first code

In your class write the following code:

package main.java.com.myitbasics.initial;

public class SimpleHelloWorld {

    public static void main(String[] args) {
        System.out.println("MyItBasics: Simple Hello World!");
        System.exit(0);
    }

}

Now you have a class ready to be executed

Code analysis

package main.java.com.myitbasics.initial;

A package is a grouping of related types that provides access protection and namespace management.

Note that types refer to classes, interfaces, enumerations, and annotation types. The package name refers explicitly to the project directories in which the class is contained.

public class SimpleHelloWorld {

A class is a definition (blueprint) of the data and available procedures that operates with defined data types or class of objects. Classes can contain procedures or functions (methods) and data (variables).

For additional information about what a class is, please visit this site.

public static void main(String[] args) {

This is a method definition. A method is a procedure that defines a behavior over data or a behavior of the class. The main method is the method that the Java compiler lookup to execute.

The signature of the method must be public static void main with the parameter String[] (array of strings) in order to be executed by Java.

System.out.println("MyItBasics: Simple Hello World!");

This line will print in the standard output (by default the Console) the text enclosed in the parenthesis.

System.exit(0);

This line executes a method that terminates the Java virtual machine with a successful code in order to correctly close all used resources. Is not a mandatory line.

Execute your first code

There are different ways to execute your Java class from RHJDS. However, the simpler and straightforward way is:

Right-click on the class -> Run As -> Java Application

Your results

To see the result of your first Java class execution you have to look up the Console view. There you will see the text “MyItBasics: Simple Hello World!” which is the expected output.

Leave a Reply

Your email address will not be published. Required fields are marked *