Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
AAA
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?
Garbage Collection
When 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
Coming up: String Methods
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) {…}
• public int foo (int other, int names){…}
• private int foo (int a, int b){…}
return type irrelevant
param names irrelevant
modifier irrelevant
Visibility Modifiers
public
Enforce
encapsulation
Variables
Methods
4-13
private
Provide services
to clients
© 2004 Pearson AddisonWesley. All rights reserved
Support other
methods in the
class
Let’s go over the examples
4-14
© 2004 Pearson AddisonWesley. All rights reserved
Questions?
4-15
© 2004 Pearson AddisonWesley. All rights reserved