Creating your first Java Class

Your First Java Class: HelloWorld.java

What you will need:

In order to create your first Java Class, the Helloworld class, you will need:

  • The latest Java Development Kit(or JDK) – The JDK contains the java compiler (javac.exe)
  • A simple text  editor like Microsoft Notepad, Notepad++ etc.,

Creating the Helloworld class:

1. Open up the text editor (e.g. Notepad) and enter in the code below (or simply copy and paste it into notepad).

/**

* @Name: Helloworld.java

* @version: 1.0

*

*/

class Helloworld

{

//The main method - the entry point for execution of your program

public static void main(String[] arguments)

{

//the code for your main method goes here

System.out.println("This is my First Java Class!");

}

}

2. Save the file as Helloworld.java

RULE: Java is case-sensitive, so make sure the ‘H’ in Helloworld is a capital letter as spelt here when you save it.

RULE:Whatever you have named the class, this must be the same name you use to save your file.

Compiling the Helloworld class:

3. Next you will need to compile Helloworld.java. To do this in Windows 7(the details will be different for other versions of windows), Click on the start orb, and enter cmd.exe into the search box to bring up a DOS Command window.

4. When the DOS window opens go to the address of the JDK (The default address is C:\Program Files\Java\jdk1.8.0_05)

5. Type javac Helloworld.java

Running the Helloworld.class file:

To run the Helloworld.class file you will need a program called java.exe. But you dont have to worry about that becuase it comes with the JDK. So at the command prompt, just enter java Helloworld, and voila you should get the output: This is my first Java Class!.

Congratulations!

 
Next on this Trail: instance variables
 


Leave a comment