Java for RPG Programmers
Authors: Coulthard P. Farr G.
Published year: 2002
Pages: 31-32/191
Buy this book on amazon.com >>

JAVA Accessor Rights

Now that you have seen all of the Java constructs (variables, methods , classes, and packages), we can formally summarize what the access modifiers are and what they do. You can divide the world up into the classes that make up your package, and other classes that use your package. By default, all classes, class variables, instance variables , and methods are accessible by all the code in this package—and only the code in this package. This is called "package" access control, and it's what you get if you don't specify any access modifiers. This is equivalent to specifying the EXPORT keyword on your procedures in RPG, but not specifying EXPORT (* ALL ) on CRTSRVPGM or identifying the procedure in your binder language source. You can further qualify the level of accessibility by using access control modifiers:

  • public . All code is allowed access to this variable, method, or class. This is the only modifier allowed for classes.

  • protected . Only this class or classes that extend this class are allowed access to this variable, method, or constructor. (Extending a class is discussed in Chapter 9.)

  • private . Only the code inside this class is allowed access to this variable or method. This modifier is not permitted on classes or constructors.

Table 2.5 compares each of these access rights to RPG IV procedure access rights. Remember that in RPG, all procedures (and fields) that specify the EXPORT keyword are available to all other modules in the program or service program (package access), while only the subsets listed in the binding source are available for users of the service program (public access), unless EXPORT (* ALL ) is used, in which case all procedures with the EXPORT keyword are available for users of the service program. If you do not even specify the EXPORT keyword, only code in this module can call the procedure.

Table 2.5: Java Method Access Modifiers versus RPG Procedures

Java

RPG

package (default)

EXPORT keyword, but not in binding source or no
EXPORT(*ALL)

public

EXPORT keyword, and in binding source or EXPORT(*ALL)

protected

not applicable

private

no EXPORT keyword



Summary

Phew! You've covered a lot of ground in this chapter, both from an RPG IV point of view and from a Java point of view. For Java, you have learned that:

  • An application is comprised of Java source files ( .java ) that are compiled via javac into Java .class files containing bytecode.

  • Source files contains one class (typically) with instance variables and methods that use those variables .

  • Flow of control starts with a Java class containing a main method, and continues via object instantiation and object method calls through to other Java classes.

  • You instantiate a class instance into an object using the new operator, which returns the address of that object.

  • Variables of a class type are called object reference variables.

  • To call a method or access a variable in an object, use the dot operator.

  • Two Java methods in the same class can have the same name, as long as their signatures are different (overloading). A method signature is defined as the name , the number of parameters, and the type of the parameters, collectively.

  • Variables that are not declared with the static modifier are called instance variables.

  • Variables declared with the static modifier are called class variables and have the same value across all objects of the class. They can be accessed by qualifying with the class name versus an object reference variable.

  • Methods declared with the static modifier are called class methods. They can be accessed by qualifying with the class name versus an object reference variable.

  • Java classes can have constructors, which are implicitly called by the Java runtime when an object is created with the new operator. These have the same name as the class and do not specify a return type. They can be overloaded.

  • The main method gets control from the command line, and must have an explicit signature and modifiers: public static void main(String args[]) .

  • You can pass parameters from the command line, and the main method gets them as entries in the args array of String objects.

  • You can write to the console by passing any variable, string literal, or plus-operator-separated list to the method System.out.println() .

  • Java classes are grouped using packages, which you identify with the package statement and access with the import statement.

  • Java allows you to explicitly define who can instantiate your class: code in this package (the default) or all code (the public modifier).

  • Java allows you to explicitly define who can call methods and who can read or write variables: all code ( public ), code in this package (the default), code in extending classes ( protected ) or code in this class only ( private ).

  • You should make all instance variables private , and supply get and set methods to retrieve or set the value.


Java for RPG Programmers
Authors: Coulthard P. Farr G.
Published year: 2002
Pages: 31-32/191
Buy this book on amazon.com >>