Thursday, February 9, 2012

A first Java application

1.       Basic Java application elements
2.       The main method in Java
3.       Compiling and running a Java application
4.       Including a Java class in a package
5.       Setting the classpath
6.       Summary

Basic Java application elements
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:
·         a comment
o   You 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.
o   You indicate a comment in Java by preceding the comment text with //.
·         A class definition
o   To 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.
o   You enclose a class definition with curly brackets or braces. The class definition can include methods and variables.
o   The 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
o   The FirstApp class 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.
o   The main method is used to start the application.
·         The main method’s implementation code
o   Curly brackets enclose the main method’s implementation code.
o   In 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 main method 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 main method 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:
  • public
    • The public modifier indicates that any class can call the method.
  • static
    • 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.
  • void
    • The void modifier indicates that the method will not return any value
The main method 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.


0 comments:

Post a Comment