Beginning Java with program class, main function and comments

PUBLISHED Jul 31st, 2018

We have already installed the Java Development Kit (JDK) for our system. With this article, we are beginning Java programming by writing a simple Hello Java program, also learning how to use the java command line tools.

Beginning Java with program class, main function and comments

If you haven’t installed JDK yet, you can read the complete tutorial here, you also need to understand some basic concepts of object oriented programming which we have already discussed here.

Hello Java class

We are beginning java with the simple Hello, World program which we have already tested in the last article. So now we can discuss it in detail.

As we have discussed in previous articles that Java is a pure object oriented language, therefore, everything in Java is an Object.

Even the whole program itself is an object.

So now suppose, if our program is an object, it has to belong to some class also. Let us call it a class Hello.

In Java, we conventionally name classes with first letter capital. So its Hello, not hello.
  • The name of this class should also be the name of the file.

Beginning java with main function

Now there is a catch. In a java program, there can be multiple objects of multiple classes having multiple methods etc. How would java know from where to execute the program? Therefore, it is made a rule that, java program begins with the class having the main function.

So if there are a number of classes, in a program, the class which has the main function will be executed first. And therefore, every java program has at least one class, which has the main function.

This main function is usually declared as follows:

<class_name_of_program> {
    public static void main(String args[]){

Now let’s add some functionality. How about our Java code say something output? “Hello Java!”. So to print something on screen, we can use a function called system.out.println().

System.out.println(" <your text here> ");

Now add all this up into a complete program.

  • Our class name is Hello.
  • So our file name should also be Hello.
  • There will be a main function in it.
  • That main function will print Hello Java!.
class Hello {
    public static void main(String args[]){
        System.out.println("Hello Java!");

javac and java

Saving it as, now we will compile it. To compile we will use javac which is Java Compiler. It takes our java files and converts it into java class which have bytecodes that are understood by Java (Java Virtual Machine to be precise, we will understand these concepts later).

So, open your command prompt and do:


If you are having any issues in the above command, or you don’t know what the heck is this, it is the compilation process which was setup and explained in the last part.

After using the javac command, it will create a Hello.class file for us. Now all we need to do is to run it using java command. So in the same directory, do:

java Hello

And this brings the output as follows:

Hello Java!
Just for Fun: Check out collection of Hello World program in every language at this awesome website

Adding comments

Doesn’t our code look so plain and dry? We can add comments to add important remarks and explanation for different parts of the code as follows.

There are two types of comments.

The block comments, in which we can comment out multiple lines between /* and */

/* whatever
written inside me
will get

And the second type is line comment. So anything written after // will get ignored. It works for a single line only.

int x; //this is x and I am comment!
float y;

So adding comments to our original code, we have

Found at ramdeoshubham! LOL

class Hello {
    public static void main(String args[]){
        System.out.println("Hello Java!");
    } // Every bracket should be closed properly.

Saving it and running it, we will see No Change. Obviously, because comments are ignored!

So this was a brief explanation of a simple hello world program. Hello World programs always help, even in beginning java. If you have any doubts, please feel free to comment down below.

In the upcoming articles, we will discuss how to create multiple classes and use them. Until then, keep learning programming from zero at

You might also like:


(rand) Creating your first HTML webpage
(rand) Binary Number System
(rand) What is a Database?

© Shubham Ramdeo, 2020
you are really awesome!