|
1. Introduction | |
2. Can we include non-static members in an application class? | |
2. Can we define an object as a constant? |
The modifiers static and final are used to indicate class members and constant members respectively. Note that a constant can be either a class (static) or an instance member. On this page the use of these modifiers is expanded upon by considering some "Frequently Asked Question" (FAQs) concerning these modifiers.
In these pages our main method has always been incorporated into what we have called an application class (i.e. by definition an application class is one that contains a main method). For convenience we have therefore only used class methods (static methods) in our applications. In this context a commonly asked questions is:
Can we include non-static members in our application class?
The answer is "of course", but non-static members are instance members and thus can only be interacted with using an instance of our application class.
For example the code presented in Table 1 describes an application class (Doit) that includes (1) three instance fields, (2) a constructor and (3) one instance method (difference). In the application we create two instances of the application class and outputs the difference in the values associated with each field using the difference method (some sample output is presented in Table 2).
// EXPERIMENT WITH NON-STATIC MEMBERS IN AN APPLICATION CLASS // Frans Coenen // 5 November 2002 // Dept Computer Science, University of Liverpool class Doit { // ----------- FIELDS ---------- private int value1; private int value2; private int value3; // ----------- CONSTRUCTORS ---------- public Doit(int val1, int val2, int val3) { value1 = val1; value2 = val2; value3 = val3; } // ------------------ METHODS ------------------------ /* MAIN: Top level method */ public static void main(String[] args) { // Create two instances of Doit Doit object1 = new Doit(1,2,3); Doit object2 = new Doit(2,4,6); object1.difference(object2); } /* DIFFERENCE: Calculate and output difference between foelds associate with two instances of the class Doit. */ private void difference(Doit object) { System.out.println("difference in value1 = " + Math.abs(value1-object.value1)); System.out.println("difference in value2 = " + Math.abs(value2-object.value2)); System.out.println("difference in value3 = " + Math.abs(value3-object.value3)); } } |
Table 1: Application class containing non-static members
$ java Doit difference in value1 = 1 difference in value2 = 2 difference in value3 = 3 |
Table 2: Sample output from code presented in Table 1
We are familiar with the use of the keyword final to define constants of various primitive types. However, what happens if we define a constant reference variable such as an object name (or an array name). What we are describing here is an object whose reference value cannot be changed, i.e. the same object name cannot be used (through a process of assignment) to point to a different instance of the same class. But this is not to say that we cannot change the values of the fields associated with such an object using an appropriately defined method. For example the code presented in Table 3 defines a simple class with three instance fields, two instance methods and a constructor. The first method is a "set" method to change the values of the fields, while the second is used to carry out some output on the same lines as the method of the same name included in Table 2.
// A CLASS WITH 3 FIELDS, TWO METHODS AND A CONSTRUCTOR // Frans Coenen // 5 November 2002 // Dept Computer Science, University of Liverpool class Doit2 { // ----------- FIELDS ---------- private int value1; private int value2; private int value3; // ----------- CONSTRUCTORS ---------- public Doit2(int val1, int val2, int val3) { value1 = val1; value2 = val2; value3 = val3; } // ------------------ METHODS ------------------------ /* SET VALUES: Assign new values to fields associated with an instance of the class Doit2. */ public void setValues(int val1, int val2, int val3) { value1 = val1; value2 = val2; value3 = val3; } /* DIFFERENCE: Calculate and output difference between foelds associate with two instances of the class Doit2. */ public void difference(Doit2 object) { System.out.println("difference in value1 = " + Math.abs(value1-object.value1)); System.out.println("difference in value2 = " + Math.abs(value2-object.value2)); System.out.println("difference in value3 = " + Math.abs(value3-object.value3)); } } |
Table 3: Definition of a class Doit2
Table 4 defines an application class that includes two fields both of which are class instances (static instances) of the class Doit2 described in Table 3; however, the first is a constant while the second is not. The main method includes a call to the difference instance method in the Doit2 class, followed by:
Note that we cannot reassign to the object1 instance because it is a constant, however the setValues method can be used in conjunction with either instance.
Some sample output produced by the application class presented in Table 4 is given in Table 5.
// APPLICATION CLASS EXPERIMENT WITH A CONSTANT OBJECT // Frans Coenen // 5 November 2002 // Dept Computer Science, University of Liverpool class Doit2app { // ----------- FIELDS ---------- private static final Doit2 object1 = new Doit2(1,2,3); private static Doit2 object2 = new Doit2(2,4,6); // ------------------ METHODS ------------------------ /* MAIN: Top level method */ public static void main(String[] args) { object1.difference(object2); // Reassign values to fields belonging to object1 object1.setValues(3,6,9); // Assign new reference value to object2 object2 = new Doit2(4,8,12); // Call difference with new assignments object1.difference(object2); } } |
Table 4: Definition of an application class including an object constant
$ java Doit2app difference in value1 = 1 difference in value2 = 2 difference in value3 = 3 difference in value1 = 1 difference in value2 = 2 difference in value3 = 3 |
Table 5: Some sample output from code presented in Table 4
Created and maintained by Frans Coenen. Last updated 06 November 2002