Writing Code: Creating an Application

Here's the sample Java application that I'll develop through to the compiling and running stages over the next few sections. Place this code in a file named ch10_01.java:

 public class ch10_01  {     public static void main(String[] args)     {         System.out.println("Welcome to Java");     } } 

As we've seen, this application will print out the text Welcome to Java when you run it. For example, here's how things would look in a DOS window under Windows:

 C:\>java ch10_01  Welcome to Java 

As you can see, this is not the most powerful of programs, but it's simple enough to get us started. We'll work up from this point to windowed Java applications at the end of the chapter. To see what's going on in ch10_01.java, I'm going to take it apart line by line now.

public class ch10_01

Note the first line in ch10_01.java:

 public class ch10_01  {     .     .     . } 

This line of code says that we're creating a new Java class named ch10_01 . When we translate this file into a bytecode file, Java itself will create an object of this class and give it control.

Note also the keyword public in this line of code. This keyword is an access specifier . When you use the public access specifier for a class, that class is accessible anywhere in your program. The main class for a Java application must always be public . In fact, Java insists that you name the file after the public class in it, which is why this file is named ch10_01.java (note that capitalization countsch10_01.java must hold a public class named ch10_01 , not Ch10_01 or cH10_01 ). Because the name of a public class sets the name of the file where that class is defined, you can have only one public class in a file.

Following the public class ch10_01 line is the actual implementation of the class, which goes in curly braces:

 public class ch10_01  {   .   .   .  } 

As with the code you write for methods , the code you write for objects must go inside curly braces. The first line of code in the curly braces is coming up next.

public static void main(String[] args)

The next line of code in the application is as follows :

 public class ch10_01  {  public static void main(String[] args)  {         .         .         .     } } 

What's going on here? In this case, I'm defining a function that's part of an object (because it's defined inside the object's definition), which makes it a method. I'm also declaring this method public, which means that it's accessible (may be called) outside the object. Methods that I declare private cannot be called from outside the object (and are usually utility methods that other methods inside the object call). As with functions in JavaScript, you can pass arguments to Java methods, and you can have methods return values. I'll go into more detail on this process later in the chapter, but here I'm indicating that this method is named main and does not return any value, which I specify with the void keyword. The main method is a special one in Java because it is called automatically when Java starts this application. When Java finds the main method, it passes control to it (applets don't need a main methodin fact, that's a major programming difference between programming applets and applications). You place the code that you want run as soon as the application starts in the main method.

In addition, I'm indicating that this method is passed an array of Java String objects by enclosing the code String[] args in parentheses after the method name. You must declare the type of every argument you pass to a method. I'm listing that type as String[] here, which is an array of strings. I'm also naming that array args , which is how I can refer to it in the method's code. This array is passed to every application's main method. As we'll see later in this chapter, you can use it to read the command-line arguments passed to the application. (For example, if you were to start the application like this %java ch10_01 Welcome to Java , the command-line arguments are Welcome , to , and Java .)

There's one more point here: Note the static keyword. Technically, the main method is a method of the application's main class, ch10_01 . You don't create an object of the ch10_01 class yourself in code; it remains as a class. For that reason, the methods and data items in the ch10_01 class are class methods and data items (as opposed to object methods and data items). There is a rule for class methods and data items: They must be declared static , which gives Java a special way of storing them. When you've declared them static , you have access to the methods and data items in a class without having to create an object of that class.

This line of code starts the main method; the rest of this method's code is inside curly braces, as usual:

 public class ch10_01  {     public static void main(String[] args)  {   .   .   .   }  } 

The purpose of this method is to print the text Welcome to Java , and I'll do that in the next line of code.

System.out.println("Welcome to Java");

The main method has just one line of code in it, and here it is:

 public class ch10_01  {     public static void main(String[] args)     {  System.out.println("Welcome to Java");  } } 

This line of code is the only one that actually produces anything as far as the user sees; it prints Welcome to Java . So what's going on here?

In this case, I'm using some of the built-in functionality that comes with Java. Like JavaScript, Java has plenty of classes and objects ready for you to use. In Java, that functionality is available in Java packages (which are class libraries). One of the Java packages that is available in any Java program is java.lang, the Java language package itself. This package makes an object named System available to us, which itself contains an object named out that lets you communicate with the user. In particular, I'm using the out object's println method here to display text on the user's console.

Here's another thing to notice: This line of code ends with a semicolon ( ; ). Ending each simple statement with a semicolon has become standard in languages such as C, C++, Java, and even JavaScript. In JavaScript, we were able to omit the semicolon because browsers don't require it, and most people do. In Java, it's another story: The semicolons are required. In fact, if you're coming to Java from JavaScript, you might get the feeling that Java is a very prickly language by comparison to JavaScript. Not only do you have to put in the semicolons, but you also have to specify a data type for each data item. When you try to assign a value of one type to a variable of another (which might be legal in JavaScript), Java will give you warning and error reports .

At this point, you've created your new application and stored it in a file named ch10_01.java. What's the next step? How do you get it to actually run? Take a look at the next section.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net