Java Tutorial – First Java Class, Compiling and Execution
Java Tutorial – First Java Program, Compiling and Execution
In this tutorial we will work on creating our very first Java program. We will discuss and dissect the different parts of a java program. In addition, we will be compiling our java program using javac the java compiler into bytecode. Finally, we will will execute our simple application and show the results.
What’s Covered
- My First Java Class (MyFirstClass.java)
- Output of My First Java Class
- Dissecting the Structure of the Java Source File
- Compiling our Java Source File
- Executing our Compiled Java Class
My First Java Class (MyFirstClass.java)
public class MyFirstClass { public static void main(String[] args) { System.out.println("Welcome to my first java program..."); } }
Output of My First Java Class
Welcome to my first java program...
Dissecting the Structure of the Java Source File

Structure of the Java Class in detail
- Create a class called MyFirstClass
- Declare the class as public
- Use the keyword class to create an object of that type
- The first character of the class’s name is uppercased and remaining characters are lowercased.
- Our class’s body is populated with fields and methods
- System.out.println() will output to the console whatever message is in quotes.
- In order for our class to be executable, it must contain a main() method.
main() Method Signature
In order for the JVM to correctly execute the main() method of a Java program it must contain the keywords public, static, and void.
Compiling our Java Source File
- Save the Java source file(s)
- In order to compile our Java source file you will need to go into the directory where our source files reside
- Compile the Java source file(s) by using the javac command followed by the file you wish to compile.
- Use the following command: javac MyFirstClass.java
- If everything goes well, you will see a new file being created with a *.class extension.
- We should now see: MyFirstClass.class
- This *.class file will contain bytecode in a format that the JVM will be able to execute
The following example below will show you the commands I ran on a Windows system to create MyFirstClass.class.
C:\workspace_core\MyFirstApp\src>dir 03/05/2015 06:14 PM 143 MyFirstClass.java C:\workspace_core\MyFirstApp\src>javac MyFirstClass.java C:\workspace_core\MyFirstApp\src>dir Directory of C:\workspace_core\MyFirstApp\src 03/05/2015 06:20 PM 453 MyFirstClass.class 03/05/2015 06:14 PM 143 MyFirstClass.java
Executing our Compiled Java Class
Now that our compiler has created and compiled our .class file we are ready to execute that class file and in our environment.
C:\workspace_core\MyFirstApp\src>java MyFirstClass Welcome to my first java program...

Core Java Related Tutorials
- Base64 Encoding and Decoding Examples in Java 8
In this tutorial we will discuss how to Encode and Decode using Base64 using Java 8, which now finally has native Base64 support. - Base64 Encoding and Decoding Examples in Java using Google Guava
This tutorial will introduce how to Encode and Decode using Base64 using Google’s Guava Project Open Source library. - Base64 Encoding and Decoding Examples in Java using Apache Commons
This tutorial will introduce Base64 encoding and decoding examples using the Apache Commons Codec library. - Custom Number Formatting in Java
In this example we will show you how to use the NumberFormat and DecimalFormat classes to format numbers using special patterns. - Custom Date Formatting in Java
In this example we will show you how to use the SimpleDateFormat class to format Date objects using special patterns to better fit the needs of the application.
Please Share Us on Social Media






Leave a Reply