You can create a Java source code file by writing the code in a text editor.
This code is a simple Java application that prints out the message “Your first Java application”.
The source code for the application consists of:
oYou can use comment to make your code easy for other programmer to understand and use. A comment is not part of an application’s code, so it’s ignored by the compiler.
oYou indicate a comment in Java by preceding the comment text with //.
·A class definition
oTo create a class, you use the class keyword and the name of the class – in this case, it’s FirstApp. In this example, you use the access modifier public to indicate that the class can be accesses from anywhere.
oYou enclose a class definition with curly brackets or braces. The class definition can include methods and variables.
oThe naming convention in Java is to start class names with suppercase letters. Java is case sensitive, so all identifiers – such as class names – must appear in exactly the same case through the code.
·A main method
oThe FirstAppclass has a single method, known as the main method. Every Java application must include at least one class with a main method declared in a standard way.
oThe main method is used to start the application.
·The main method’s implementation code
oCurly brackets enclose the mainmethod’s implementation code.
oIn this application, the main method simply prints a string – “Your first Java application” – to the standard output. The line ends with a semicolon. In Java you use the semicolon as a separator, to tell the compiler where each statement ends.
A Java file can contain only one public class. If the source file uses a public class, you must save it using the name of the public class with a .java extension. In this example, you name the file FirstApp.java.
Because Java is case sensitive, it is essential to name the file with exactly the same characters in uppercase or lowercase as the name you used for the class.
The main method in Java
When you run a Java application, the interpreter calls the main method. The main method then calls all the other methods required to run the application.
The presence of the mainmethod is what differentiates this piece of code from a regular class, making it into an executable piece of code that can be run by the Java interpreter. The mainmethod must be declared public to enable the Java interpreter to access it.
All Java applications must have main method with a precise signature. The signature contains three modifiers:
The public modifier indicates that any class can call the method.
The static modifier indicates that the main method can be called without needing an instance of the class
These static methods are known as class method.
The void modifier indicates that the method will not return any value
The mainmethod always takes an array of type String as an argument. You can use this array to pass command line arguments to a program at runtime.
Note: The name of the array can be any valid Java identifier, but args is used by convention.