package examples

Download Report

Transcript package examples

MIT AITI 2004 – Lecture 11
Packages, Access, and Scope
Access & Organization
• Let’s say you have 100’s of files on your
PC. What kind of problems do you
encounter?
– can’t find particular files
– duplicate or similar file names
– hard to keep personal files private
• Solution: Sort and organize your files into
subdirectories
Packages
• Organize your classes into sets or units
• Reduce problems with name conflicts
• Identify your classes in a set with specific
functionality
How to specify the package for a class:
package <package name>;
Using Packages
• A class can always access other classes
in its own package
• A class can always access public
classes in other packages
Q: What happens if we don’t specify a
package as public or private?
A: the default level of access is package
Levels of Access Control
Visibility
private package
public
From the same
class
yes
(default)
yes
From any class
in same package
no
yes
yes
From any class
outside the
package
no
no
yes
yes
Big line to cross!
Import Classes and Packages
• Specify the class you want to import:
import java.util.Date;
• You can import multiple classes:
import java.util.ArrayList;
• You can also import whole packages:
import java.util.*;
• Default imported package:
import java.lang.*;
Package Example: Person.java
package examples;
class Person {
String name;
// add a field to store a birthday
// write a method to set the birthday
// write a method to get the birthday
}
Using java.util.Date
package examples;
class Person {
String name;
java.util.Date birthday;
void setBirthday(java.util.Date d) {
birthday = d;
}
java.util.Date getBirthday() {
return birthday;
}
}
Importing java.util.Date
package examples;
import java.util.Date;
class Person {
String name;
Date birthday;
void setBirthday(Date d) {
birthday = d;
}
Date getBirthday() {
return birthday;
}
}
Importing java.util.ArrayList
package examples;
import java.util.Date;
import java.util.ArrayList;
class Person {
String name;
Date birthday;
ArrayList friends;
void setBirthday(Date d) {
birthday = d;
}
// . . .
}
Importing java.util.*
package examples;
import java.util.*;
class Person {
String name;
Date birthday;
ArrayList friends;
void setBirthday(Date d) {
birthday = d;
}
// . . .
}
Packages Quiz 1
package example1;
public class A {
public int a = 5;
}
package example1;
public class B {
int b = 5;
}
package example1;
public class C {
private int c = 5;
}
package example1;
public class quiz{
void main() {
A ant = new A();
B bug = new B();
C cat = new C();
// Which are correct?
int testa = ant.a;
int testb = bug.b;
int testc = cat.c;
}
}
Package Quiz 2
package example1;
public class A {
public int a = 5;
}
package example1;
public class B {
int b = 5;
}
package example1;
public class C {
private int c = 5;
}
package example2;
import example1.*;
public class quiz{
void main() {
A ant = new A();
B bug = new B();
C cat = new C();
// Which are correct?
int testa = ant.a;
int testb = bug.b;
int testc = cat.c;
}
}
SCOPE
Scope of a Variable
• Definition: the block (section) of code for
which your variable (identifier) exists
• The scope is set to the block in which you
defined your variable
• This is the block of code where you can
use the variable
Scope Quiz 1
class TestScope {
int x = 0;
void f() {
int y = 20;
x = 10;
}
void print() {
System.out.println(x);
f();
System.out.println(x);
System.out.println(y);
}
}
What is the output
of print()?
This file won't
compile, so print
will never execute
Method Scope
class TestScope {
int x = 0;
void f() {
int y = 20;
x = 10;
}
• x is defined for the
whole class block.
• y is defined inside
the method f().
void print() {
System.out.println(x);
f();
System.out.println(x);
System.out.println(y); // ERROR
}
}
Scope Quiz 2
class TestScope {
int x = 0;
void f() {
int y = 20;
x = 10;
}
void print() {
int y = 0;
System.out.println(x);
f();
System.out.println(x);
System.out.println(y);
}
}`
Does this fix the
problem?
What is the output
of print()?
0
10
0
Scope Quiz 3
class TestScope {
int x = 0;
int y = 0;
void f() {
int y;
y = 20;
x = 10;
}
Now, we declare a
new field, y.
What is the output of
print()?
void print() {
0
System.out.println(x);
10
f();
0
System.out.println(x);
System.out.println(y);
}
}
Scope Quiz 4
class TestScope {
int x = 0;
int y = 0;
void f() {
y = 20;
x = 10;
}
Now, we change the
method f().
What is the output of
print()?
void print() {
0
System.out.println(x);
10
f();
System.out.println(x);
20
System.out.println(y);
}
}
Scope Quiz 5
package examples;
class Operations{
int square(int a) {
a = a * a;
return a;
}
• What is the output of
print()?
25 <- square(a)
5 <- a
void print() {
int a = 5;
System.out.println(square(a));
System.out.println(a);
}
}
Scope Quiz 6
package examples;
class Operations{
int a = 5;
int square(int a) {
a = a * a;
return a;
}
• What is the output of
print()?
25 <- square(a)
5 <- a
void print() {
System.out.println(square(a));
System.out.println(a);
}
}
Scope Quiz 7
package examples;
class Operations{
int a = 5;
int square(int a) {
this.a = a*a;
return a;
}
• What is the output of
print()?
5 <- square(a)
25 <- a
void print() {
System.out.println(square(a));
System.out.println(a);
}
}
Loop Scope
void adding() {
for (int j = 0; j<5; j++) {
int sum += j;
}
System.out.println(sum);
}
• What’s wrong with the above segment of code?
ERROR: sum is only defined inside the for loop
Loop Scope Fixed
void adding() {
int sum = 0;
for (int j = 0; j<5; j++) {
sum += j;
}
System.out.println(sum);
}
• sum is now defined for the whole block of
code for the adding method
Summary of Scope
• Definition: the block (section) of code for
which your variable (identifier) exists
• The scope is set to the block in which you
defined your variable
• This is the block of code where you can
use the variable