More About Objects and Methods Chapter 5 1

More About Objects and Methods Chapter 5 1

Auto-Boxing and Unboxing and Wrapper Classes • Many Java library methods work with class objects only – Do not accept primitives – Use wrapper classes instead! • Java 5. 0 will auto-box your primitives for you (and unbox them as necessary) – Works on parameters as well Integer int. Object = 5; int i = int. Object; Chapter 5 2

Operation Overloading • It is sometimes useful to have two methods of the same name but accept different parameters (very common in Math library) • May not differ only by return type or variable name • Constructors can be overloaded like other methods Valid: public String f(int x) {…} public String f(double x) {…} public String f(float f) {…} Invalid: public int f(int x) {…} public double f(int x) {…} Chapter 5 3

Constructors • Like methods, constructors can have parameters – Used to initialize class variables, set up anything else necessary – Different constructors may initialize object to different values • No return type • Java provides a default constructor for you if you do not declare any constructor yourself Chapter 5 4

Copy Constructors and Memory Management • Copy constructors are useful in making a deep copy of an object – Shallow copy: copy primitives, simple reference assignment with “=” for objects • What problems may crop up with using simple assignments “=”? – Deep copy: copy over all values and objects explicitly into new memory Chapter 5 5

Memory Management public class A { A a 1 = new A(); A a 2 = new A(2); a 1 = a 2; a 2. set(3); A a 3 = new A(a 1); a 1. set(4); private int x; public A() { x=0; } public A(int x) { set(x); } public A(A a) { x = a. get(); } public void set(int x) { this. x = x; } public int get() { return x; } a 1. get()? a 2. get()? a 3. get()? } Chapter 5 6

static keyword • Useful for methods which do not need to access class variables or methods – Called using the class name – Think: Math library – Static methods cannot access non-static class methods or variables! (compilation error) • Used for class variables which are shared over instances of the class (three people share a bowl of chips, one person eats some chips, all parties lose chips) class A { private static int a; public void set. A(int x) { a = x; } public int get. A() { return a; } A a 1 = new A(); A a 2 = new A(); a 1. set. A(5); System. out. println(“a: “+a 2. get. A()); } Chapter 5 7

static Memory Management public class A { A a 1 = new A(); A a 2 = new A(2); a 1 = a 2; A. set(3); A a 3 = new A(a 1); a 1. set(4); a 3. set(1); private static int x; public A() { x=0; } public A(int x) { set(x); } public A(A a) { x = a. get(); } public static void set(int x) { this. x = x; } public static int get() { return x; } } Chapter 5 a 1. get()? a 2. get()? a 3. get()? 8

null keyword • Indicates the non-existence of an object (think address 0 x 0000) • Can be used to represent any object type • A null object can NOT call any methods – This will result in a runtime error called a Null. Pointer. Exception – Get in the habit of checking against the null object! • Since a reference address is simply a number, use == or != to compare Chapter 5 9

null Memory Management public class A { private int x; public A() { x=0; } public A(int x) { set(x); } public A(A a) { x = a. get(); } public void set(int x) { this. x = x; } public int get() { return x; } } Chapter 5 A a 1 = null; a 1 = new A(); A a 2 = new A(a 1); A a 3 = null; if (a 3 == null) a 3 = new A(); else a 3. set(3); a 1. set(4); a 1. get()? a 2. get()? a 3. get()? 10

Case Study: Point. java Chapter 5 11

Adding Buttons • A component in an applet that looks like a push button and can do something when it is clicked is referred to as a button. • Buttons are added to applets the same way labels are added. • But, unlike labels, actions can be associated with buttons. Chapter 5 25

Creating Buttons • example JButton sunny. Button = new JButton(“Sunny”); • (Until buttons are programmed, they do not perform any action besides depressing and returning to their undepressed state. ) Chapter 5 27

Event-Driven Programming • Applets use events and listeners. • An event is an object that represents some action such as clicking a mouse button. • An object fires (or generates) an event. • An object that can fire an event can have one or more listener objects, specified by the programmer. Chapter 5 30

Event-Driven Programming, cont. • A listener object can have methods called event handlers, defined by the programmer, that specify what happens when events are sent to the listener. – “sent” means that some method is invoked automatically with the event object as an argument. Chapter 5 31

Event-Driven Programming, cont. Chapter 5 32

Event-Driven Programming, cont. • Events determine the order in which things happen. – The “next” thing to happen is determined by the next event. Chapter 5 33

Event-Driven Programming, cont. • class Button. Demo Chapter 5 34

Event-Driven Programming, cont. Chapter 5 35

Programming Buttons • The applet class definition needs to know two things: – for each button, which objects are listeners (called registering the listener) – the defined method(s) to be invoked when the event is sent to the listener Chapter 5 36

Programming Buttons, cont. • registering the listener sunny. Button. add. Action. Listener(this); – The class Button. Demo itself is the listener class. Chapter 5 37

Listeners • Different kinds of components require different kinds of listener classes. • Buttons generate action events which are handled by action listeners. • An action listener is an object of type Action. Listener. – Action. Listener is not a class, but is instead an interface which we will discuss in Chapter 7. Chapter 5 39

Listeners, cont. – Add the phrase implements Action. Listener to the beginning of the class definition – Define a method named action. Performed. – Often it is convenient to put the method action. Performed in the applet it is intended to change. – Because the applet itself is the action listener, the action event goes to the applet’s object, where it is passed automatically to the method action. Performed. Chapter 5 40

Listeners, cont. – Method action. Performed typically needs to know the source of the action event. e. get. Action. Command() returns the string written on the button which can be used to determine the source. • An import statement is needed to define an action listener class. import java. awt. event. *; Chapter 5 41

Applets and Constructors • Applets normally have no constructors. • Method init is used for any needed initialization. Chapter 5 42

Icons • An icon typically is a small picture. • Pictures can be produced in several formats for display on the screen (e. g. GIF and JPEG). • These pictures can be used as the basis for an icon. • A label or a button can include a string and/or an icon. Chapter 5 43

Icons, cont. • Example Image. Icon duke. Icon = new Image. Icon(“duke_waving. gif”); nice. Label. set. Icon(duke. Icon); Chapter 5 44

Icons, cont. class Icon. Demo Chapter 5 45

Icons, cont. • Buttons can have icons. Image. Icon smiley. Face. Icon = new Image. Icon(“smiley. gif”); sunny. Button. set. Icon(smiley. Face. Icon); Chapter 5 47

Icons, cont. Chapter 5 48

Changing Visibility • Labels, buttons, and other components have a method set. Visibility to make the component visible or invisible. – If the argument is true, the component is visible. – If the argument is false, the component is invisible (and no longer present). Chapter 5 49
- Slides: 31