Chapter 7. Java for RPG programmers 317
These four integer variables are all different entities in Java. The RPG compiler folds these
names to uppercase and treats them as a single entity.
Comments may begin with either the C style of a slash followed by an asterisk and end with
an asterisk followed by a slash, or the C++ style of a double slash:
/* The first line of a C style comment line
** The second line of a C style comment line
** The final line follows ....
// A C++ style comment line
// Another C++ style comment line
Java is also a strongly typed language. This means that all named entities in the program
must have a specific type (for example, char, int, Object, and so on). The type is always
specified when declaring a variable in Java.
7.1.3 Object creation
Every class in Java has at least one constructor method. This is a method with the same
name as the class. It may or may not accept arguments. An object is created by creating a
new instance of a class:
Thing myThing = new Thing();
This statement performs both the definition and the declaration of a variable. The definition is
the part to the left of the equal sign and says define a variable called myThing that is a type of
Thing. The declaration is the part to the right of the equal sign and says create a new Thing.
The equal sign is an assignment statement. The statement creates a new Thing and stores
the reference to that new object in the variable myThing.
Thing() is the default constructor for the Thing class and does not accept any arguments. It is
possible for a class to have multiple constructors each accepting different arguments. A
constructor is used to initialize a new object.
7.1.4 Class variables
Class variables are those with a static modifier. These variables are associated with the class
and, therefore, are accessible from every instance of the class. They are used where the
variable represents something that is either independent of each object or is dependent on all
objects. For example, a counter of the number of objects (instances of a particular class) can
be implemented as a class variable and increased by the constructor for that class giving all
objects knowledge of how many of them exist:
static int howMany = 0;
7.1.5 Class methods
Class methods also use the static modifier. You can start these methods without an instance
of the class having been created. This may be necessary where the methods operate on one
or more of the various primitive data types. For example, the Math class (in java.lang)
declares all its methods as static, because no object is required to use the Math functions.
They all accept numeric primitive data types. They also do not reference any object instance
double aRoot = Math.sqrt(someValue);