aaa. today’s lecture review of chapter 6 go over examples

15
AAA CS 211 static References Methods Scope, Encapsulation

Upload: randell-chase

Post on 17-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

AAA

CS 211staticReferencesMethodsScope, Encapsulation

Today’s lecture

• Review of Chapter 6• Go over examples

Chapter 6 review

•What is a static method? (A static variable?)• How does Java handle assignment (during method calls)?•What is scope? Local? Parameter? Instance? Class?•What is visibility? Public? Private?

Copying parameters Example•What value gets printed?

public class Test { public static void main(String[] args) { int x=3; changeVal(x,5); System.out.println(x); } public static void changeVal(int p, int v) { p = v; }}

("3" is printed.)

But what does this mean for objects?•What gets printed?

public class Test { public static void main(String[] args) { Person p = new Person("Mason",21); changeName(p,"Thomas"); System.out.println(p.getName()); } public static void changeName(Person p, String n) { p.setName(n); }}

Thomas (Java copies the reference)

How about now?•What gets printed?

public class Test { public static void main(String[] args) { Person p = new Person("Mason",21); changeName(p,"Thomas"); System.out.println(p.getName()); } public static void changeName(Person p, String n) { p = new Person(n,30); }}

Mason (new object created/modified/discarded)

Does it matter that we have a local variable called p in changeName? What if it were called q?

How about now? (v2)•What gets printed?

public class Test { public static void main(String[] args) { Person p = new Person("Mason",21); p = changeName(p,"Thomas"); System.out.println(p.getName()); } public static Person changeName(Person q, String n) { q = new Person(n,30);

return q; }}

"Thomas" (new object created/modified/returned, THEN p is updated to point to it.)

Does it matter that we have a local variable called p in changeName? What if it were called q?

Reference Assignment continued

•Why are the references copied, instead of the entire object?•Objects can be big• Wastes space• Takes a long time to copy

•What can be a downside to copying just the reference?•Now you have two things pointing to the same location in memory• What happens if you forget this mistakenly?

Creating lots of objects

We know how to create and allocate memory to programs with new in Java

Could our program ever run out of memory?◦Not if it is super small◦What if you run something for weeks that is constantly

creating objects it only uses once?Want a way to retrieve (recover) memory that is no longer being used◦Turns out this is tricky! How do we know an object is no longer

needed?

Coming up: String Methods

Garbage CollectionWhen an object no longer has any valid references to it, it can no longer be accessed by the program

The object is useless, and therefore is called garbage

Java performs automatic garbage collection periodically, returning an object's memory to the system for future use

In other languages, the programmer is responsible for performing garbage collection

Method Overloading (Quick View)

We can have multiple methods with the same name in one class! They are distinct methods with no actual relations other than the name – this is just a mnemonic convenience!

To coexist, their method signatures must be uniquely identifiable by the parameter types and method name alone.• parameter names are ignored – arguments are nameless.• return type is ignored – not explicit via method call, so can't

help select correct method.

Constructors are methods too – we can write multiple constructors for one class, as long as they have different signatures.• This is a valuable opportunity (something Python disallows).

Method Overloading - Examples

Example: these methods may all be in one class:

1. public int foo (int a, int b){…}

2. public int foo (char a, int b){…}

3. public int foo (int b, char a){…}

4. public int bar (int a, int b){…}

5. public String foo (int a, int b, int c){…}

The following could not be added to the class:• public String foo (int a, int b) {…} return type irrelevant• public int foo (int other, int names){…} param names

irrelevant• private int foo (int a, int b){…} modifier irrelevant

© 2004 Pearson Addison-Wesley. All rights reserved4-13

Visibility Modifiers

public private

Variables

MethodsProvide services

to clients

Support othermethods in the

class

Enforceencapsulation

Violateencapsulation

© 2004 Pearson Addison-Wesley. All rights reserved4-14

Let’s go over the examples

© 2004 Pearson Addison-Wesley. All rights reserved4-15

Questions?