Print Hello World in Java Programming
Learn How To Print Hello World in Java Programming Language with 3 Different Methods. We have tried to cover up the basic program in Java Programming. Here, we have also Explained How the Program To Display Hello World works in Java.
Method 1: Declaring the Class containing Main Method as Public
1 2 3 4 5 6 7 | public class HelloWorld { public static void main(String args[]) { System.out.println("Hello World\n"); } } |
Note: When the Class containing the Main Function is declared as Public in a Java Program, then the Class Name should be same as the Java File Name.
Also Read: Java Code To Check Even and Odd Numbers
Method 2: Not Declaring the Class containing Main Method as Public
1 2 3 4 5 6 7 | class HelloWorldProgram { public static void main(String args[]) { System.out.println("Hello World\n"); } } |
Note: When Public Access Modifier is not used in the Class containing the Main Method in a Java Hello World Program, then the Class Name may be different to the Java File Name.
Method 3: Declaring Class without Main Method
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.io.*; class HelloWorld { static { System.out.println("Hello World in Static Block\n"); } public static void main(String args[]) { System.out.println("Hello World in Main"); } } |
Also Read: Java Program To Find Factorial of a Number
Steps To Execute Java Programs in Linux Ubuntu
- Create a Text File (using gEdit) named HelloWorld.java on Linux Ubuntu Desktop.
- Paste the above code into the Text file and Save it with a .java extension.
- Open Ubuntu Terminal by pressing Ctrl+Alt+T.
- Type: cd Desktop and Hit Enter.
- Type: javac HelloWorld.java and Hit Enter. This step helps in Compiling Java Programs.
- To Execute the program, use the command java HelloWorld.
- Now, you would be able to see the Output as Hello World on the Terminal.
Output

How To Run Hello World in Java in Linux Ubuntu Video Tutorial
In case you get any Compilation Errors with this Code To Print Hello World in Java Programming or if you have any doubt about it, mention it in the Comment Section.
Thanks for Explanation.
We hope you could execute this Hello World Java Program perfectly!
Excellent Explanation of the variations.
You’re welcome Rajesh.