Java The Complete Reference, Seventh Edition
You should call the file that contains this programBoxDemo.java, because themain( )method is in the class calledBoxDemo, not the ...
The output produced by this program is shown here: Volume is 3000.0 Volume is 162.0 As you can see,mybox1’s data is completely s ...
Here,class-varis a variable of the class type being created. Theclassnameis the name of the class that is being instantiated. Th ...
Chapter 6: Introducing Classes 111 Assigning Object Reference Variables Object reference variables act differently than you migh ...
This is the general form of a method: type name(parameter-list) { // body of method } Here,typespecifies the type of data return ...
class BoxDemo3 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); // assign values to my ...
114 Part I: The Java Language a method, there is no need to specify the object a second time. This means thatwidth,height, andde ...
// get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); } } As you can see, whenvolume( )is c ...
Now,square( )will return the square of whatever value it is called with. That is,square( )is now a general-purpose method that c ...
Chapter 6: Introducing Classes 117 height = h; depth = d; } } class BoxDemo5 { public static void main(String args[]) { Box mybo ...
You can rework theBoxexample so that the dimensions of a box are automatically initialized when an object is constructed. To do ...
Chapter 6: Introducing Classes 119 As you can see, bothmybox1andmybox2were initialized by theBox( )constructor when they were cr ...
class BoxDemo7 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mybox1 = new Box( ...
Chapter 6: Introducing Classes 121 Instance Variable Hiding As you know, it is illegal in Java to declare two local variables wi ...
122 Part I: The Java Language such situations, Java provides a mechanism calledfinalization.By using finalization, you can defin ...
Chapter 6: Introducing Classes 123 To see a practical application of the preceding discussion, let’s develop one of the archetyp ...
// push some numbers onto the stack for(int i=0; i<10; i++) mystack1.push(i); for(int i=10; i<20; i++) mystack2.push(i); / ...
7 A Closer Look at Methods and Classes 7 A Closer Look at Methods and Classes T his chapter continues the discussion of methods ...
126 Part I: The Java Language // Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b ...
// Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } // overl ...
«
3
4
5
6
7
8
9
10
11
12
»
Free download pdf