Java Reference
In-Depth Information
between its declaration and the closing brace (the } symbol). You cannot use
this msg variable before that, or anywhere else in your program. You might
have another variable named msg somewhere else, but it will be a totally dif-
ferent variable, with different values. This msg is visible and usable only
locally, in this one block of code. We say that its scope is local.
The parameters that you declare in a function are considered local as well.
Here in the declaration for the helloCommand function:
public void helloCommand(MessageReceiver caller, String[] parameters) {
the variables caller and parameters are all available within this function, but
aren't visible anywhere else. They are local.
Most of the time local variables are all you need. In fact, local variables are
pretty safe to use. No other part of the program can use or change them, and
it's very clear what line of code set a local variable's value and where it is
used.
Global Variables
But you can make variables that have a wider scope and aren't just local (and
we're going to need to do that for our plugins in this chapter).
Maybe you've declared a variable that many different functions can use. Maybe
your entire class, or even the entire program and all your libraries, can see
it and change it. We call that a global variable.
We've used this before, but you haven't seen it yet. Let's take a sneak peek
inside EZPlugin , which we used in the very first HelloWorld plugin. It has a class-
level static variable declared right at the top, named logger .
EZPlugin/src/com/pragprog/ahmine/ez/EZPlugin.java
public class EZPlugin extends Plugin implements CommandListener {
/* Boilerplate methods for all of our plugins */
public static Logman logger;
public EZPlugin() {
logger = getLogman();
}
@Override
public boolean enable() {
logger.info ( "Starting up" );
We make the call to getLogman and ask it for the logger object. It returns that
to us, and we assign it to our variable named logger . logger is of type Logman ,
 
 
 
Search WWH ::




Custom Search