Java isn't difficult but the little details of compiling and packaging keep tripping me up so it's time to take a more rigorous (otherwise known as boring) approach.
Sun's Java tutorials are a good starting point. Religiously following the hello world examples takes only a few minutes and even if it doesn't teach you anything new it helps to crystallise the thoughts you have about the things that the tutorial hasn't told you so that you can pursue those things in a less scatterbrained manner (at least that's how it feels to me).
This is a confusing section because it talks about class and instance methods and variables without explaining the differences in the declarations. That is, it explains how to use already defined class methods but doesn't tell you how to create your own even though the hello world application does exactly that.
Applets do not need to implement a main method. The lesson thereby implies that they could if they wanted to but doesn't expand on it.
Applets must extend Applet so they must import java.applet.Applet or the extends statement must fully qualify the Applet class.
public class HelloWorld extends java.applet.Applet {
If you want to use many classes from a package then you might want to just import the whole package; do this by using an asterisk instead of the class name:
import java.applet.*;
All the stuff in java.lang is imported by default so
System and its friends do not need an import statement.
None of what we have learnt so far explains what a package is from our point of view as application programmers nor does it explain how or why we might create our own packages.
At last, this is where I normally trip up and fail to get a runnable program.
Exercise 5 has two classes in two separate .java files. When compiled you get two separate .class files. If there had been more than one class in each .java file there would be one .class file for each class. To run the program you execute java with SecondClass as the argument:
SLOCOMBE# pwd f:/Users/kjw/java/tutorials/cupojava SLOCOMBE# java SecondClass English (United Kingdom)
If you change to another directory and try what seems obvious to run the class again you will see that it fails:
SLOCOMBE# pwd
f:/Users/kjw/java/tutorials/cupojava
SLOCOMBE# cd ..
SLOCOMBE# java cupojava/SecondClass
java.lang.NoClassDefFoundError: cupojava/SecondClass (wrong name: SecondClass)
at java.lang.ClassLoader.defineClass0(Native Method)
...
This further reinforces the lesson that the java compiler wants a class name not a filename.