Java Tutorials
Java Methods
Java Classes
Java File Handling
In the previous chapter, we created a Java file called Main.java
, and used the following code to print "Hello World" on screen:
Main.java
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Every line of code running in Java should be within the class
. In our example, we named the class Main. The class should always start with the first capital letters.
Note: Java is very sensitive: "MyClass" and "myclass" have a different meaning.
The java file name must match the class name. When saving a file, save it using the class name and add ".java" to the end of the filename. To use the example above on your computer, make sure Java is properly installed: Go to Start Chapter to find out how to install Java. The output must be:
Hello World
The main()
method is required and you will see it throughout Java application:
public static void main(String[] args)
Any code within the main()
method will be used. You do not need to understand keywords before and after the main. You will get to know them little by little as you read this lesson.
In the meantime, just remember that every Java application has a class
name that must be the same as the filename, and that the entire program must contain a main()
method.
Within the main()
method, we can use the println()
method to print a line of text on the screen:
public static void main(String[] args) {
System.out.println("Hello World");
}
Note: Folded brackets {}
mark the beginning and end of the code block.
Note: Each code statement must end with a semicolon.