Creating your first Java Application


                We now basically have a rough idea about what Java is and how to set it up on our machine. If you haven't gone through the introduction, you can read it here: Introduction. The next step is to actually begin writing your code. Open your favorite text-editor (remember it must be a plain-text editor and not something like MS Word) and type in the following code:

     public class MyApp {
         public static void main(String[] args) {
             System.out.println("Wow, I just made my first application in Java!");
         }
     }


Save this program in a file called MyApp.java. Now open Command Prompt and go to the directory where you have saved this file. Type: ‘javac MyApp.java’ (without the quotes). A file called ‘MyApp.class’ will be created in that directory. Now just type: ‘java MyApp’ (again, without the quotes of course). If everything goes well, the program should run fine.

C:\> javac MyApp.java                          # ‘javac’ compiles the file.
C:\> java MyApp                                # ‘java’ runs the compiled file.
C:\> Wow, I just made my first application in Java!         # Output

Absolute beginners might be a little disoriented on seeing this code. So, let’s explain it in chunks.

Here’s the first line:
     public class MyApp { . . . }

This line mainly declares a class called MyApp. The actual functioning code is put within the curly braces following the class name. You might have noticed the public keyword prior to class. It is an access specifier and indicates that the MyApp class can be accessed by any other classes without any restrictions. If you omit the public keyword, the compiler will automatically place the public keyword upon compiling. However, if you explicitly write the public keyword, as in the case above, the compiler insists that you name the program file with the same name as your public class. In this case, it’d be MyApp.java. A program file can only have one explicitly declared public class, however, there are no restrictions to the number of classes you have in your programs. The syntax for class declaration is:
<access specifier (optional)> class <classname> { // your code... }

The second line:
     public static void main(String[] args) { . . . }

We’re basically creating a method or a function here. A method is like a small functional unit of the program. Each method should particularly do a certain kind of job. It is just like a tiny processor. You pass data to the method; the method processes the data and returns the result. The function name ‘main’ is more like a keyword, because the main function acts as the entry point of the program. When the JVM is set to run a program, it actually looks for the main function and then executes the code within it.

The main method should always be declared public since it needs to be accessible outside its class. It is then followed by the static keyword, which makes the main method a class method. This means you can run and access the main method without creating an object first. The main method should not return any value and hence the void keyword. Finally, note the argument within the parentheses followed by main: String[] args. This is an argument list that can be used by the method to do certain tasks based on the arguments. For example, if you write: java MyApp Hello I’m here. Then “Hello”, “I’m” and “here.” would be the three arguments that would be stored in the args String array. However, we’re not using any command-line arguments with our application and hence you won’t find any usage of the args variable within the code.

The third line:
     System.out.println("Wow, I just made my first application in Java!");

This is our first statement in the complete code. A statement in Java is terminated by a semi-colon ‘;’. We’re actually making use of one of the pre-defined classes of Java here. Java provides lots of pre-defined classes and packages to be used by the programmers. All of these classes are available in the Java library or the Java API (Application Programming Interface). The above class is present in the java.lang package. This package contains all the classes which are essential for writing any programs. Thus this package is already imported by default and you don’t need to explicitly import it.

The java.lang package contains the System class. The System class in turn contains a field (data member) called out, and this field in turn has a method called println (stands for print line) which actually prints the value passed to it. To refer to the out field of the System class we write System.out and to call the println method of the out field we write System.out.println. To print the text we simply pass it as an argument to the println function by enclosing it in quotes.

That’s it! Go ahead and give it a try, and you’re done with your first Java application.

Comments

Popular Posts