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.
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.