Personal tools
You are here: Home Information Systems Java Platform Introduction 21. Miscellaneous Topics

21. Miscellaneous Topics

Document Actions
  • Send this
  • Print this
  • Content View
  • Bookmarks

Miscellaneous Topics

<-- Previous

Table of Contents

Next -->

Public variables

So far we have worked mainly with private variables in our classes. Public variables can be declared, which declaration makes those variables visible from outside the class. This can be useful in certain circumstances, in particular where the class includes a constant value which needs to be visible from outside the class. However, there is a danger to declaring variables accessible from another class - if they are not protected or final, other classes can change their value directly:

/* changing the value of an unprotected 
public instance variable */
aClass.pubVar = 13;

If the code in the class does not expect the user to change the value of this variable, errors will result. We must therefore be very circumspect when considering public variables for any class we are defining.

This does not mean that we should never use public variables - they are very useful. For example, look up javax.swing.JOptionPane in the Javadocs, and then click on the link to the FIELD summary:

Here you will see a summary of the public variables accessible in the javax.swing.JOptionPane class. Did you notice that most of them are static or protected variables? Their capitalisation tells us that they are mostly final (constant) values too, which means that it is safe for them to be declared public because even though they can be accessed directly, they cannot be changed - they're final. This provides the measure of protection needed in order for these variables to safely be declared public.

Many of these variables return values that are used as arguments to constructors, e.g. JOptionPane.YES_NO_OPTION is an integer constant that tells the showConfirmDialog() method which buttons to include on the message dialog. Allowing users to access this value directly from the class means that users do not have to know what the actual value is in order to produce the right kind of message dialog - they can simply use the JOptionPane.YES_NO_OPTION class variable:

JFrame frame = newJFrame();
int result = JOptionPane.showConfirmDialog(frame,
		"Do you wish to exit the application?",
		"Quit?",
		JOptionPane.YES_NO_OPTION);

As you can see, we now have a message dialog with 'Yes' and 'No' buttons. Thereafter, we can use another public variable value to see which button was pressed in the message dialog:

if(result == JOptionPane.YES_OPTION){
    System.exit(0);
}

protected Access

The access level protected lies between public and private. If a class is both a subclass of and in the same package as the class with the protected member, then the class has access to the protected member. We do not deal with protected members in this tutorial, but they are mentioned here for completeness' sake.

package Paranoia

public class Protective {
   protected boolean iAmProtected;
   
   public Protective(){
     iAmProtected = true;
   }

   protected void protectedMethod() {
     System.out.println("iAmProtected is : " + iAmProtected);
   }
}

public class Sheltered extends Protective {
   protected static boolean protectedToo = true;
   
   public Sheltered(){
     super();
   }
   
   public void accessProtectedMethod(){
     System.out.println();
     this.protectedMethod();  //legal - Sheltered extends Protective
   }
}

package laissezFaire

public class Denied {
   private boolean peek;
   
   public Denied(){
     peek = Sheltered.protectedToo;  //illegal - no access to protected var
   }
}

The usefulness of public variables becomes apparent, but in using them in the classes you build just remember to be mindful of the kind of variable that you declare public, and make sure that any possible unexpected changes to the value of such variables cannot cause errors. Alternatively, you might need to use a public variable whose value you fully expect other classes to modify, so that the class can do something useful with the modified value. In general however, public variables should be used only when truly necessary or unavoidable.

<-- Previous

Table of Contents

Next -->

Copyright 2007-2008, by the Contributing Authors. Cite/attribute Resource. 21. Miscellaneous Topics. (2008, July 16). Retrieved June 19, 2013, from UWC Free Courseware Web site: http://free.uwc.ac.za/freecourseware/information-systems/java-platform-introduction/miscellaneous-topics. This work is licensed under a Creative Commons License : Attribution-ShareAlike 3.0. Creative Commons License : Attribution-ShareAlike 3.0