19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

312 Chapter 8 Objects and Classes<br />

create a frame<br />

add panel <strong>to</strong> frame<br />

display frame<br />

37 panel.add(jlblName); // Add the label <strong>to</strong> the panel<br />

38 panel.add(jtfName); // Add the text field <strong>to</strong> the panel<br />

39 panel.add(jchkBold); // Add the check box <strong>to</strong> the panel<br />

40 panel.add(jchkItalic); // Add the check box <strong>to</strong> the panel<br />

41 panel.add(jrbRed); // Add the radio but<strong>to</strong>n <strong>to</strong> the panel<br />

42 panel.add(jrbYellow); // Add the radio but<strong>to</strong>n <strong>to</strong> the panel<br />

43 panel.add(jcboColor); // Add the <strong>com</strong>bo box <strong>to</strong> the panel<br />

44<br />

45 JFrame frame = new JFrame(); // Create a frame<br />

46 frame.add(panel); // Add the panel <strong>to</strong> the frame<br />

47 frame.setTitle("Show GUI Components");<br />

48 frame.setSize(450, 100);<br />

49 frame.setLocation(200, 100);<br />

50 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br />

51 frame.setVisible(true);<br />

52 }<br />

53 }<br />

This program creates GUI objects using the classes JBut<strong>to</strong>n, JLabel, JTextField,<br />

JCheckBox, JRadioBut<strong>to</strong>n, and JComboBox (lines 6–31). Then, using the JPanel class<br />

(line 34), it then creates a panel object and adds the but<strong>to</strong>n, label, text field, check box, radio<br />

but<strong>to</strong>n, and <strong>com</strong>bo box <strong>to</strong> it (lines 35–43). The program then creates a frame and adds the<br />

panel <strong>to</strong> the frame (line 45). The frame is displayed in line 51.<br />

✓Point✓ Check<br />

8.14 How do you create a Date for the current time? How do you display the current time?<br />

8.15 How do you create a JFrame, set a title in a frame, and display a frame?<br />

8.16 Which packages contain the classes Date, JFrame, JOptionPane, System, and<br />

Math?<br />

Key<br />

Point<br />

8.7 Static Variables, Constants, and Methods<br />

A static variable is shared by all objects of the class. A static method cannot access<br />

instance members of the class.<br />

VideoNote<br />

Static vs. instance<br />

instance variable<br />

static variable<br />

static method<br />

The data field radius in the circle class is known as an instance variable. An instance variable<br />

is tied <strong>to</strong> a specific instance of the class; it is not shared among objects of the same class.<br />

For example, suppose that you create the following objects:<br />

Circle circle1 = new Circle();<br />

Circle circle2 = new Circle(5);<br />

The radius in circle1 is independent of the radius in circle2 and is s<strong>to</strong>red in a different<br />

memory location. Changes made <strong>to</strong> circle1’s radius do not affect circle2’s radius,<br />

and vice versa.<br />

If you want all the instances of a class <strong>to</strong> share data, use static variables, also known as<br />

class variables. Static variables s<strong>to</strong>re values for the variables in a <strong>com</strong>mon memory location.<br />

Because of this <strong>com</strong>mon location, if one object changes the value of a static variable, all<br />

objects of the same class are affected. <strong>Java</strong> supports static methods as well as static variables.<br />

Static methods can be called without creating an instance of the class.<br />

Let’s modify the Circle class by adding a static variable numberOfObjects <strong>to</strong> count the<br />

number of circle objects created. When the first object of this class is created,<br />

numberOfObjects is 1. When the second object is created, numberOfObjects be<strong>com</strong>es 2.<br />

The UML of the new circle class is shown in Figure 8.13. The Circle class defines the<br />

instance variable radius and the static variable numberOfObjects, the instance methods<br />

getRadius, setRadius, and getArea, and the static method getNumberOfObjects.<br />

(Note that static variables and methods are underlined in the UML class diagram.)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!