In just about every lesson up to this point you have been creating Java applications-writing classes, creating instance variables and methods, and running those applications to perform simple tasks. Also up to this point, you've focused either on the very broad (general object-oriented theory) or the very minute (arithmetic and other expressions). Today, you pull it all together and learn how and why to create classes by using the following basics:
Defining classes is pretty easy; you've seen how to do it a bunch of times in previous lessons. To define a class, use the class keyword and the name of the class:
class MyClassName {
/* body of class */}
If this class is a subclass of another class, use extends to indicate the superclass of this class:
class myClassName extends mySuperClassName {
/* body of class */
}
If this class implements a specific interface, use implements to refer to that interface:
class MyRunnableClassName implements Runnable {
/* body of class */
}
Both extends and implements are optional. You'll learn about using and defining interfaces in Week 3.
A class definition with nothing in it does nothing; usually, when
you create a class, you
have something you want to add to make that class different from
its superclasses. Inside
each class definition are declarations and definitions for variables,
or methods, or both-for the class and for each instance
of the class. In this section, you'll learn all about instance
and class variables; the next section talks about methods.
On Day 4, you learned how to declare and initialize local variables-that is, variables inside of method definitions. Instance variables, fortunately, are declared and defined in almost exactly the same way as local variables; the main difference is their location in the class definition. Variables are considered instance variables if they are declared outside of a method definition. Customarily, however, most instance variables are defined just after the first line of the class definition. For example, Listing 7.1 shows a simple class definition for the class Bicycle, which inherits from the class PersonPoweredVehicle. This class definition contains four instance variables:
Listing 7.1. The bicycle class.
1: class Bicycle extends PersonPoweredVehicle {
2: String bikeType;
3: int chainGear;
4: int rearCogs;
5: int currentGearFront;
6: int currentGearRear;
7: }
Constants are useful for defining shared values for all the methods of an object-for giving meaningful names to object-wide values that will never change. In Java, you can create constants only for instance or class variables, not for local variables.
A constant variable or constant is a variable whose value never changes (which might seem strange given the meaning of the word "variable").
To declare a constant, use the final keyword before the variable declaration and include an initial value for that variable:
final float pi = 3.141592;
final boolean debug = false;
final int maxsize = 40000;
| Technical Note |
The only way to define constants in Java is by using the final keyword. Neither the C and C++ constructs for #define nor const are available in Java, though const is reserved to help catch its accidental use. |
Constants can be useful for naming various states of an object and then testing for those states. For example, suppose you have a test label that can be aligned left, right, or center. You can define those values as constant integers:
final int LEFT = 0;
final int RIGHT = 1;
final int CENTER = 2;
The variable alignment then also is declared as an int:
int alignment;
Then, later on in the body of a method definition, you can either set the alignment:
this.alignment = CENTER;
or test for a given alignment:
switch (this.alignment) {
case LEFT: // deal with left alignment
...
break;
case RIGHT: // deal with right alignment
...
break;
case CENTER: // deal with center alignment
...
break;
}
As you learned in previous lessons, class variables are global to a class and to all of that class's instances. You can think of class variables as being even more global than instance variables. Class variables are good for communicating between different objects with the same class, or for keeping track of global states among a set of objects.
To declare a class variable, use the static keyword in the class declaration:
static int sum;
static final int maxObjects = 10;
Methods, as you learned on Day 5, define an object's behavior-what happens when that object is created and the various operations that the object can perform during its lifetime. In this section, you'll get a basic introduction to method definition and how methods work; tomorrow, you'll go into more detail about advanced things you can do with methods.
Method definitions have four basic parts:
The method's signature is a combination of the name of the method, the type of object or base type this method returns, and a list of parameters.
| Note |
To keep things simple today, I've left off two optional parts of the method definition: a modifier such as public or private, and the throws keyword, which indicates the exceptions a method can throw. You'll learn about these parts of a method definition in Week 3. |
In other languages, the name of the method (or function, subroutine, or procedure) is enough to distinguish it from other methods in the program. In Java, you can have different methods that have the same name but a different return type or argument list. This is called method overloading, and you'll learn more about it tomorrow.
Here's what a basic method definition looks like:
returntype methodname(type1 arg1, type2 arg2, type3 arg3..) {
/* body of the method */
}
The returntype is the primitive type or class of the value this method returns. It can be one of the primitive types, a class, or void if the method does not return a value at all.
Note that if this method returns an array object, the array brackets can go either after the return type or after the parameter list; because the former way is considerably easier to read, it is used in the examples today (and throughout this book):
int[] makeRange(int lower, int upper) {...}
The method's parameter list is a set of variable declarations, separated by commas, inside parentheses. These parameters become local variables in the body of the method, whose values are the objects or values of primitives passed in when the method is called.
Inside the body of the method you can have statements, expressions,
method calls to other objects, conditionals, loops, and so on-everything
you've learned about in the previous lessons. You will create
a new project from Table 7.1 that reviews all of this material.
| Step | Actions needed |
| 1. Name project | Project name: Range.prj
Directory: ..\Cafe\Projects |
| 2. Set project type | Check option Release Change Target Type to Application |
| 3. Add files to project | No changes |
| 4. Initial settings | No changes |
If your method has a real return type (that is, it has not been declared to return void), somewhere inside the body of the method you need to return a value. Use the return keyword to do this. Notice the return statements while you key in Listing 7.2 and save the file as RangeClass.java. After you're done, add it to the project Range. RangeClass.java shows an example of a class that defines a makeRange() method. makeRange() takes two integers-a lower bound and an upper bound-and creates an array that contains all of the integers between those two boundaries (inclusive).
Listing 7.2. The RangeClass class.
1:class RangeClass {
2: int[] makeRange(int lower, int upper) {
3: int arr[] = new int[ (upper - lower) + 1 ];
4:
5: for (int i = 0; i < arr.length; i++) {
6: arr[i] = lower++;
7: }
8: return arr;
9: }
10:
11: public static void main(String arg[]) {
12: int theArray[];
13: RangeClass theRange = new RangeClass();
14:
15: theArray = theRange.makeRange(1, 10);
16: System.out.print("The array: [ ");
17: for (int i = 0; i < theArray.length; i++) {
18: System.out.print(theArray[i] + " ");
19: }
20: System.out.println("]");
21: }
22:
23:}
After compiling and running the project here is what the output will look like:
The array: [ 1 2 3 4 5 6 7 8 9 10 ]
The main() method in this class tests the makeRange() method by creating a range (see line 3) where the lower and upper boundaries of the range are 1 and 10, respectively (see line 15), and then uses a for loop to print the values of the new array.
In the body of a method definition, you might want to refer to the current object-the object the method was called on-to refer to that object's instance variables or to pass the current object as an argument to another method. To refer to the current object in these cases, you can use the this keyword. this refers to the current object, and you can use it anywhere that object might appear-in dot notation to refer to the object's instance variables, as an argument to a method, as the return value for the current method, and so on. Here's an example:
t = this.x; // the x instance variable for this object
this.myMethod(this); // call the myMethod method, defined in
// this class, and pass it the current
// object
return this; // return the current object
In many cases, however, you might be able to omit the this keyword. You can refer to both instance variables and method calls defined in the current class simply by name; the this is implicit in those references. Therefore, the first two examples could be written like this:
t = x; // the x instance variable for this object
myMethod(this); // call the myMethod method, defined in this
// class
| Note |
Omitting the this keyword for instance variables depends on whether there are no variables of the same name declared in the local scope. See the next section for details. |
Keep in mind that because this is a reference to the current instance of a class, you should therefore only use it inside the body of an instance method definition. Class methods-that is, methods declared with the static keyword-cannot use this.
When you refer to a variable within your method definitions, Java checks for a definition of that variable first in the current scope (which might be a block), then in the outer scopes up to the current method definition. If that variable is not a local variable, Java then checks for a definition of that variable as an instance or class variable in the current class; and then, finally, in each superclass in turn.
Java makes it possible in certain situations for you to create a variable in a lower scope to hide a variable in a higher scope. When the definition of a variable in a lower scope is exactly the same as a variable in a higher scope, the value for higher variable is hidden. This, however, is not a recommended programming practice because it can introduce subtle and confusing bugs into your code.
For example, note this small Java program:
class ScopeTest {
int test = 10;
void printTest () {
int test = 20;
System.out.println("test = " + test);
}
}
In this class, you have two variables with the same name and definition. The first, an instance variable, has the name test and is initialized to the value 10. The second is a local variable with the same name, but with the value 20. Because the local variable hides the instance variable, the println() method will print that test is 20.
You can circumvent this by using this.test to refer to the instance variable, and just test to refer to the local variable.
A more insidious example of this occurs when you redefine a variable in a subclass that already occurs in a superclass. This can create very subtle bugs in your code-for example, you might call methods that are intended to change the value of a certain instance variable, but instead change a lower scope variable and cause the value of the higher one to be left unaffected. Another bug might occur when you cast an object from one class to another-the value of your instance variable might mysteriously change (because it was getting that value from the superclass instead of from your class). The best way to avoid this behavior is to make sure that, when you define variables in a subclass, you're aware of the variables in each of that class's superclasses and you don't duplicate what already is there.
When you call a method with object parameters, the variables you pass into the body of the method are passed by reference, which means that whatever you do to those objects inside the method affects the original objects as well. This includes arrays and all of the objects that arrays contain; when you pass an array into a method and modify its contents, the original array is affected. (Note that primitive types are passed by value.)
Here's an example to demonstrate how this works. First, you have a simple class definition, which includes a single method called onetozero() (see Listing 7.3).
Listing 7.3. The PassByReference class.
1:class PassByReference {
2: int onetoZero(int arg[]) {
3: int count = 0;
4:
5: (int i = 0; i < arg.length; i++) {
6: if (arg[i] == 1) {
7: count++;
8: arg[i] = 0;
9: }
10: }
11: return count;
12: }
13:}
The onetoZero() method does two things:
Now create a project and add the main()
method to your class in Listing 7.3 to make a runnable example.
Create a new project called PassByReference
from the information in Table 7.2
| Step | Actions needed |
| 1. Name project | Project name: PassByReference.prj
Directory: \Cafe\Projects |
| 2. Set project type | Check option Release
Change Target Type to Application |
| 3. Add files to project | No changes |
| 4. Initial settings | No changes |
Listing 7.4 shows the main() method for the PassByReference class, which tests the onetoZero() method. Save Listing 7.4 as PassByReference.java and add it to the project PassByReference.
Listing 7.4. The code for PassByReference.java.
1:class PassByReference {
2: int onetoZero(int arg[]) {
3: int count = 0;
4:
5: for (int i = 0; i < arg.length; i++) {
6: if (arg[i] == 1) {
7: count++;
8: arg[i] = 0;
9: }
10: }
11: return count;
12: }
13:
14: public static void main (String arg[]) {
15: int arr[] = { 1, 3, 4, 5, 1, 1, 7 };
16: PassByReference test = new PassByReference();
17: int numOnes;
18:
19: System.out.print("Values of the array: [ ");
20: for (int i = 0; i < arr.length; i++) {
21: System.out.print(arr[i] + " ");
22: }
23: System.out.println("]");
24:
25: numOnes = test.onetoZero(arr);
26: System.out.println("Number of Ones = " + numOnes);
27: System.out.print("New values of the array: [ ");
28: for (int i = 0; i < arr.length; i++) {
29: System.out.print(arr[i] + " ");
30: }
31: System.out.println("]");
33: }
34:}
Here is the output of this project:
Values of the array: [ 1 3 4 5 1 1 7 ]
Number of Ones = 3
New values of the array: [ 0 3 4 5 0 0 7 ]
Let's go over the main() method line by line so that you can see what is going on.
Lines 16 through 18 set up the initial variables for this example. The first one is an array of integers; the second one is an instance of the class PassByReference, which is stored in the variable test. The third is a simple integer to hold the number of ones in the array.
Lines 20 through 24 print out the initial values of the array; you can see the output of these lines in the first line of the output.
Line 26 is where the real work takes place; this is where you call the onetoZero() method, defined in the object test, and pass it the array stored in arr. This method returns the number of ones in the array, which you then will assign to the variable numOnes. The method onetoZero() returns three, as you would expect.
Line 27 prints out the number of ones-that is, the value you got back from the onetoZero() method.
The last bunch of lines print out the array values. Because a reference to the array object is passed to the method, changing the array inside of that method changes that original copy of the array. Printing out the values in lines 28 through 32 proves this-the last line of output shows that all the 1s in the array have been changed to 0s.
Just as you have class and instance variables, you also have class and instance methods, and the difference between the two types of methods is analogous. Class methods are available to any instance of the class itself and can be made available to other classes. Therefore, some class methods can be used anywhere regardless of whether an instance of the class exists or not.
For example, the Java class libraries include a class called Math. The Math class defines a whole set of math operations that can be used in any program or the various number types:
float root = Math.sqrt(453.0);
System.out.print("The larger of x and y is " + Math.max(x, y));
To define class methods, use the static keyword in front of the method definition, just as you would create a class variable. For example, that max class method might have a signature like this:
static int max(int arg1, int arg2) { ... }
Java supplies "wrapper" classes for each of the base types-for example, classes for Integer, Float, and Boolean. Using class methods defined in those classes, you can convert to and from objects and primitive types. For example, the parseInt() class method in the Integer class takes a string and a radix and returns the value of that string as an integer:
int count = Integer.parseInt("42", 10) // returns 42
| Note |
Radix (also known as base) is the name for the base of a particular number system. For example, the decimal system has a radix (or base) of 10. In the hexadecimal, the radix is 16, 8 in the octal, and 2 in the binary. |
Most methods that operate on a particular object, or affect that object, should be defined as instance methods. Methods that provide some general utility but do not directly affect an instance of that class are better declared as class methods.
Now that you know how to create classes, objects, and class and instance variables and methods, all that's left is to put it together into something that can show you how all of the topics discussed today interact with each other.
Applications, to refresh your memory, are Java programs that run on their own. Applications are different from applets, which require Netscape 2.0 or later, or a Java-capable browser to view them. Much of what you've been creating up to this point has been Java applications; next week you'll dive into how to create applets. (Applets require a bit more background in order to get them to interact with the browser and draw and update with the graphics system. You'll learn all of this next week.)
A Java application consists of one of more classes and can be as large or as small as you want it to be. HotJava is an example of a Java application. The only thing you need to make a Java application run is one class that serves as the "jumping-off" point for the rest of your Java program. If your program is small enough, it might need only the one class.
The jumping-off class for your program needs one thing: a main() method. When you run your compiled Java class (using the Java interpreter), the main() method is the first thing that gets called. None of this should be much of a surprise to you at this point; you've been creating Java applications with main() methods all along.
The signature for the main() method always looks like this:
public static void main(String args[]) {...}
Here's a rundown of the parts of the main() method:
The body of the main() method contains any code you need to get your application started: initializing variables or creating instances of any classes you might have declared.
When Java executes the main() method, keep in mind that main() is a class method-the class that holds it is not automatically instantiated when your program runs. If you want to treat that class as an object, you have to instantiate it in the main() method yourself (all of the examples up to this point have done this).
Because Java applications are stand-alone programs, it's useful to be able to pass arguments or options to that program to determine how the program is going to run, or to enable a generic program to operate on many different kinds of input. Command-line arguments can be used for many different purposes-for example, to turn on debugging input, to indicate a filename to read or write from, or for any other information that you might want your Java program to know.
To pass arguments to a Java program, you merely append them to the command line when you run your Java program:
java Myprogram argumentOne 2 three
On this command line, you have three arguments: argumentOne, the number 2, and three. Note that a space separates arguments, so this command line produces three arguments:
java myprogram Java is cool
To group arguments, surround them with double-quotes. This command line produces one argument:
java myprogram "Java is cool"
The double-quotes are stripped off before the argument gets to your Java program.
How does Java handle arguments? It stores them in an array of strings, which is passed to the main() method in your Java program. Remember the signature for main():
public static void main (String args[]) {...}
Here, args is the name of the array of strings that contains the list of arguments. You actually can call it anything you want.
Inside your main() method,
you can then handle the arguments your program was given by iterating
over the array of arguments and handling those arguments any way
you want. For example, create a new project called EchoArgs
from Table 7.3. When you're done, create a new Source window text
editor file and enter Listing 7.5 into it, then save it as EchoArgs.java.
Essentially, the class EchoArgs
is a really simple class that prints out the arguments it gets,
one per line.
| Step | Actions needed |
| 1. Name project | Project name: EchoArgs.prj
Directory: \Cafe\Projects |
| 2. Set project type | Check option Release
Change Target Type to Application |
| 3. Add files to project | No changes |
| 4. Initial settings | No changes |
Listing 7.5. The EchoArgs class.
1: class EchoArgs {
2: public static void main(String args[]) {
3: for (int i = 0; i < args.length; i++) {
4: System.out.println("Argument " + i + ": " + args[i]);
5: }
6: }
7: }
As mentioned several days ago, there are two ways to view Java applications. One way is to view them manually by opening your own DOS command-line prompt and running the java.exe command-line interpreter. The other is to use Symantec Café's Execute Program option (which opens a DOS window for you and runs java.exe). In the end it is a matter of your preference. Some people using Symantec Café might have a hard time viewing the output, because when you run a Java application through Symantec Café and it opens a command prompt, as soon as the Java application is finished it closes the window. In some cases that makes it very hard to see the output because the DOS window only comes up for a few seconds before it closes. You will receive the same overall output from a Java application no matter which way you choose to have it interpreted from.
If you are running java.exe manually from your own DOS window, you pass command arguments for this program through the following method. Make sure that you are in the directory where the class is located:
java EchoArgs 1 2 3 jump
java EchoArgs "foo bar" zap twaddle 5
But what if you wanted to pass arguments to a Java application inside of Symantec Café? You can do this by going to the Project menu item and clicking Arguments . A dialog box will open up where you can place any arguments that you want to pass to the Java application. If you take the arguments from the example above and use the command-line Java interpreter, Figure 7.1 is what your dialog box should look like.
Figure 7.1 : Passing arguments to Java programs in Cafe.
| Note |
When you enter parameters in the dialog box shown in Figure 7.1, Café will save the data that you entered between your Café sessions. |
Something to note about how the arguments are grouped in the listing: Putting quotes around foo bar causes that argument to be treated as one unit inside the argument array.
| Technical Note |
The array of arguments in Java is not analogous to argv in C and UNIX. In particular, arg[0], the first element in the array of arguments, is the first command-line argument after the name of the class-not the name of the program as it would be in C. Be careful of this as you write your Java programs. |
An important thing to note about the arguments you pass into a Java program is that those arguments will be stored in an array of strings. This means that any arguments you pass to your Java program are strings stored in the argument array. To treat them as non-strings, you'll have to convert them to whatever type you want them to be.
For example, suppose you have a very simple Java program called SumAverage that is placed in the project SumAverage. This Java program takes any number of numeric arguments and returns the sum and the average of those arguments. Listing 7.6 shows a first pass at this program.
Listing 7.6. First try at the SumAverage class with a problem.
1:class SumAverage {
2: public static void main (String args[]) {
3: int sum = 0;
4:
5: for (int i = 0; i < args.length; i++) {
6: sum += args[i];
7: }
8:
9: System.out.println("Sum is: " + sum);
10: System.out.println("Average is: " +
11: (float)sum / args.length);
12: }
13:}
At first glance, this program seems rather straightforward-a for loop iterates over the array of arguments, summing them, and then the sum and the average are printed out as the last step.
But what happens when you try to compile this? Go ahead and create
a new project based on Table 7.4, then go ahead and create the
file SumAverage.java from
Listing 7.6 and add it to your newly created project.
| Step | Actions needed |
| 1. Name project | Project name: SumAverage.prj
Directory: \Cafe\Projects |
| 2. Set project type | Check option Release
Change Target Type to Application |
| 3. Add files to project | No changes |
| 4. Initial settings | No changes |
When you actually compile and run the Java program you get the following error from the Café compiler:
Error: C:\Simple.java(6): Incompatible type for +=. Can't convert java.lang.String to int
You get this error because the argument array is an array of strings. Even though you passed integers into the program from the command line, those integers were converted to strings before they were stored in the array. To be able to sum those integers, you have to convert them back from strings to integers. There's a class method for the Integer class, called parseInt, that does just this. If you change line 6 to use that method, everything works just fine:
sum += Integer.parseInt(args[i]);
Now, compiling the program produces no errors and running it with various arguments returns the expected results. For example, java SumAverage 1 2 3 returns the following output:
Sum is: 6
Average is: 2
At this point, if you have been working on the examples with Symantec Café you might have noticed several commands related to parsing. You also could have seen something about parsed files in the Output window. For example, open the sample project Blink.prj from the directory Cafe\Samples\Java\Blink. Note the Output window as the project loads (see Figure 7.2). It is here that you will see a status report on the parsing of the project.
Figure 7.2 : Output window for Blink.prj.
The Symantec Café parser is a new technology that is the backbone of the Class Editor and Hierarchy editor. The parser is a way to get the most up-to-date information about your project without having to recompile it every time. This obviously runs as a background process and gives you the capability to work while the parsing actually is taking place. But why, exactly, does Symantec Café need the parser? In past object-oriented programming languages (particularly C++), when you changed your code but did not recompile the project, the integrated development environment would display information about your classes based on the compiled project, not the newer updated source files. Hence, there would be a time gap between what actually is saved to disk and what is being shown (particularly in a class-based viewer).
Because Java is based on the object paradigm, Symantec Café's developers created tools related to objects (that is, the Class editor and Hierarchy editor). However, Symantec did not want the time differential that was common in development environments for other programming languages. The parsing technology in Café takes advantage of the modern multithreaded operating system (for more information on multithreading, see Day 11). As a result, the parser runs on a background thread, detects when changes are made and automatically updates the parsed information. In other words, the Café parser updates information about your project automatically and in the background. Hence, when you view and work in the Class editor or the Hierarchy editor, it always will display the most up-to-date information about your project. In essence, you have the best of both worlds-a graphical object-based way to work with your Java objects that updates information about classes on-the-fly. Now that you understand how Symantec Café facilitates information to its viewing Editors and you know a little more about objects, it's time to go back to the viewing editors that were introduced earlier and work with them using a sample project that comes with Symantec Café: Blink.prj.
In this section we are going to go into more detail about the Class editor than we did on Day 2. Though we have not talked extensively about the built-in class libraries that come with Java, at this point you should have enough understanding of Java, and its objects in general, to understand the material that this section will be covering.
There is more than one way to invoke the Class editor. In Symantec Café, you often have several ways to do the same thing, giving you more ways to open the same tool, which lets you choose the one you prefer. Having more than one way to invoke the same tool enables you, for example, to start the Class editor from almost any location or workspace within Café. Though we might not cover every way to invoke the Class editor, we certainly will cover the most common ways. First, you can open the Class editor from the main window in Symantec Café by going to the Window menu on the main menu bar and clicking the Goto View sub-menu and Class Editor. You should see a menu similar to the one shown in Figure 7.3. Click the Class editor and it will be invoked. Another way to open the Class editor is from the Views palette. You also can invoke the Class editor from the Hierarchy editor (which will be talked about in the next section). Finally the third way to invoke the Class Editor is by clicking the Browsing tabbed workspace which opens not only the Class editor but the Hierarchy editor as well.
Figure 7.3 : Opening a window from the main Cafe window.
| Tip |
You can open any of the available windows (that is, tools) pertaining to Symantec Café, no matter what workspace you are currently in, through the Window menu on the main Café menu bar in the Goto View sub-menu (see Figure 7.3). |
Now that you know how to start the Class editor, we'll take a
closer look at what you can do and how you can customize it. Go
ahead and invoke it so that you see something like
Figure 7.4.
Figure 7.4 : Class editor of project Blink.prj.
First, take another look at the Classes window. Right now, you can see that with the Blink project it is displaying all of the classes related to that project sorted by order of packages. However, right-click the word Blink under the Default Package. You will see the command Go to Source. If you click this menu command, Symantec Café will invoke a separate Source window text editor that contains code for the Blink class that was highlighted. See Figure 7.5.
Figure 7.5 : Source window text editor for the class Blink (invoked from the Class editor).
| Tip |
Any time that you see the command Go to Source, clicking it will invoke a new Source window text editor with code inside it based on the item that is highlighted. |
Another command located on the pop-up menu bar in is the Settings menu command. By clicking this with the class Blink highlighted, you will invoke the Editing/Browsing Settings window that was discussed on Day 2 with the Class tab highlighted, giving you the ability to decide how you want this pane to be structured and what font you want to display these classes in. Also, there is a check box called Apply Here Only. This option enables you to make changes that only will affect this Class editor window and not carry over any of the specified changes to any of the other Class editor windows (see Figure 7.6).
Figure 7.6 : Configuring the Class editor.
| Note |
The Apply Here Only check box also is available on the Settings for the Hierarchy editor, which will be discussed in the next section. |
Focusing your attention on the Members pane-the right pane in Figure 7.4-right-click on any of the items there and you also will see the Edit Attributes menu command. Here you can specify the access modifier for the specified variable or class. For example, in Figure 7.4, right-click on the item init and you should see a dialog box like the one in Figure 7.7. Here, you can select what type of access you want for this member. At this point we will not go into too much detail because we have yet to formally go over the topic of access modifiers for Java classes.
Figure 7.7 : Class Editor changing member attributes.
At this point, you should have a better understanding of how to use the Class editor and how to customize it to your needs.
Now turn your attention to the Hierarchy editor (see Figure 7.8). The Hierarchy editor and Class editor are essentially siblings in the sense that they both take advantage of Symantec Café's parsing technology and that they overlap in functionality. For example, you can invoke the Class editor from the Hierarchy editor by double-clicking on any of the classes (that is, the rectangles).
Figure 7.8 : Hierarchy editor.
Probably the most important thing to note about the Hierarchy editor is that it is not the only window that you can work with in this editor. There are two others that we are going to talk about.
You can activate these windows by right-clicking anywhere on the Hierarchy editor's pane and clicking the menu command Settings . Just like in the Class editor, the Editing/Browsing Settings tabbed window will open up-however, this time the Hierarchy is active (see Figure 7.9).
Figure 7.9 : Settings on the Hierarchy editor.
As you can see, there are two check boxes inside of the frame Popup Windows. Click both of them (that is, Members and Source). Now click the rectangle java.awt.LayoutManager, and two windows will appear-the members window, which will display a list of members for the specified class, and the Source window, which will enable you to work with the actual code for the member specified (see Figure 7.10).
Figure 7.10 : Customized Hierarchy editor.
Today, you put together everything you've come across in the preceding days of this week about creating Java classes and using them in Java applications. This included the following:
Congratulations on completing your first week of Teach Yourself Java in 21 Days! Starting next week, you'll apply everything you have learned this week to writing Java applets, and to working with more advanced concepts in putting together Java programs and working with the standard Java class libraries.
| I tried creating a constant variable inside a method, and I got a compiler error when I tried it. What was I doing wrong? | |
| You can create only constant (final) class or instance variables; local variables cannot be constant. | |
| static and final are not exactly the most descriptive words for creating class variables, class methods, and constants. Why not use class and const? | |
| static comes from Java's C++ heritage; C++ uses the static keyword to retain memory for class variables and methods (and, in fact, they aren't called class methods and variables in C++: static member functions and variables are more common terms). final, however, is new. final is used in a more general way for classes and methods to indicate that those things cannot be subclassed or overridden. Using the final keyword for variables is consistent with that behavior. final variables are not quite the same as constant variables in C++, which is why the const keyword is not used. | |
| In my class, I have an instance variable calledname. I also have a local variable called name in a method, which, because of variable scope, gets hidden by the local variable. Is there any way to get hold of the instance variable's value? | |
| The easiest way is not to name your local variables the same names as your instance variables. If you feel you must, you can use this.name to refer to the instance variable and name to refer to the local variable. | |
| I want to pass command-line arguments to an applet. How do I do this? | |
| You're writing applets already? Been skipping ahead, have you? The answer is that you use HTML attributes to pass arguments to an applet, not the command line (you don't have a command line for applets). You'll learn how to do this next week. | |
| I wrote a program to take four arguments, but if I give it too few arguments, it crashes with a runtime error. What's wrong? | |
| Testing for the number and type of arguments your program expects is up to you in your Java program; Java won't do it for you. If your program requires four arguments, test that you have indeed been given four arguments. | |
| Is the Café parser related to the native compiler that also is specific to Symantec Café? | |
| No-the Symantec Café parser is completely separate from the native compiler that also was talked about earlier this week. The parser is used specifically by the viewing editors to give them the most up-to-date information about your classes. |