Java Programs  «Prev  Next»
Lesson 7

Java Application Construction Conclusion

This module introduced you to the basics of Java application construction by leading you through the first couple of steps of the course project. You began the module by taking a look at the Java API, which provides all of the support features necessary for building rich applets and applications. You then learned about the differences between applets and applications. From there, you focused solely on applications and learned about the main() method and command-line arguments. You wrapped up the module by examining standard input and output, which is used to input and output information in command-line applications.

More on the main() method

One of the mistakes most often made by new Java programmers is attempting to access an instance variable (which means nonstatic variable) from the static main() method (which does not know anything about any instances, so it can’t access the variable). The following code is an example of illegal access of a nonstatic variable from a static method:
class Foo {
 int x = 3;
 public static void main (String [] args) {
  System.out.println("x is " + x);
 }
}

So while you are trying to follow the logic, the real issue is that x and y cannot be used within main(), because x and y are instance variables. The same applies for accessing nonstatic methods from a static method. The rule is, a static method of a class cannot access a nonstatic (instance) method or variable of its own class.


Glossary Terms

This module discussed how the following terms relate to Java:
  1. Application Programming Interface (API): The Java API is a set of Java packages and classes included in the Java 2 Software Development Kit (SDK) that programmers use to create Java programs.
  2. Standard I/O: Standard I/O is the most basic form of input and output available to a Java program. Java standard I/O is text-based, and operates under the premise that the computer has a standard input device (keyboard) and a standard output device (monitor).
  3. main() method: The main method is a method within a Java application that is called by the Java interpreter to run the application.
  4. Command-line arguments: Command-line arguments are information passed into a command-line application that somehow controls the way the application runs.

Java Application - Quiz

Click the Quiz link below to test what you have learned in this module.
Java Application - Quiz