1.00 Lecture 11 Variable Lifecycles Scope and Access Instance (or object) variables

advertisement
1.00 Lecture 11
Scope and Access
Variable Lifecycles
• Instance (or object) variables
– Created when their containing object is created
– Initialized to default if not explicitly initialized
• 0 for numbers, false for boolean, null to objects
– Destroyed when Java® garbage collector finds there
are no remaining active references to object
• Static (or class) variables
– Created when class is first used in program
– Initialized to default if not explicitly initialized
• 0 for numbers, false for boolean, null to objects
– Usually exist for rest of program (unless unloaded)
• Local variables (or block variables)
– Created in the statement where they’re defined
– Not initialized by default. Contain unpredictable data
– Destroyed when block is exited (at ending brace )
1
Variable Scope
• Scope: limiting the parts of the program where
a variable or method is defined and visible
– Prevents collisions between variables and methods
in different parts of a program
• Variables are the big concern, methods are lesser problem
– Limits effects of changes to smallest possible
module or scope
– Lets multiple people work on large programs
simultaneously
– Allows testing, bug fixing, maintenance to be done
while limiting the new bugs introduced
Scope: Variables, Methods
• Local variables (in a method or block)
– Exist from point of definition to end of block
• Blocks are defined by curly braces{ }
• Blocks are most often used to define:
– Method body
– Multiple statements in if-else and loop operations
– Local variables exist only within own method
• No access is given to any other method
– If local and instance variable have same name,
the local variable hides the instance variable:
• Access the instance variable as:
this.variableName;
2
Access: Variables, Methods
• Instance and static variables and methods (in
a class) have 4 access modifiers:
– Private: Access only to own class’ methods
• Data fields should be private, almost always
– Public: Access to all methods, all classes
• Methods intended for other class’ use are public
• Methods for internal use only are private
– Package: Access to methods of classes in same
package (a package is a group of classes)
• This is the default, alas. Always specify scope explicitly
• No ‘package’ keyword; it’s the default with no keyword
– Protected: Used with inheritance (covered later)
• Like a private variable, except it’s visible to derived or
subclasses (and, in Java®, to other classes in package)
Access: Packages
• If you add, at the top of your program
package packageName;
– This will place the classes in your source file into
a package, along with any other source files with
the same package declaration at the top
• Packages are placed in folders in your filesystem in
Forte and on your Windows PC or Athena workstation
– These classes will then have access to each
others’ package-access methods and data fields
– Use Forte’s New Package feature as convenience
• To use the package in another class, add at
the top of that class:
import packageName.*;
3
Package Example
package Lecture11PkgClass;
public class PkgClass {
public int publicInt;
private int privateInt;
int packageInt;
// Package access
public PkgClass(int pu, int pr, int pa) {
publicInt= pu;
privateInt= pr;
packageInt= pa;
}
public void publicPrint() {
System.out.println("Public");
}
private void privatePrint() {
System.out.println("Private");
}
void packagePrint() {
// Package access
System.out.println("Package");
}
}
Package Test Class 1
package Lecture11PkgClass;
// In same package
public class PkgTest {
public static void main(String[] args) {
PkgClass object1= new PkgClass(1, 2, 3);
int pu= object1.publicInt;
int pr= object1.privateInt;
int pa= object1.packageInt;
object1.publicPrint();
object1.privatePrint();
object1.packagePrint();
System.exit(0);
}
}
// Which statements will not compile?
4
Package Test Class 1
package Lecture11PkgClass;
// In same package
public class PkgTest {
public static void main(String[] args) {
PkgClass object1= new PkgClass(1, 2, 3);
int pu= object1.publicInt;
//
int pr= object1.privateInt;
No access!
int pa= object1.packageInt;
object1.publicPrint();
// object1.privatePrint();
No access!
object1.packagePrint();
System.exit(0);
}
}
Package Test Class 2
package Lecture11TestPkg;
import Lecture11PkgClass.*;
// Different package (or none)
// Import desired package
public class PkgTest {
public static void main(String[] args) {
PkgClass object1= new PkgClass(1, 2, 3);
int pu= object1.publicInt;
int pr= object1.privateInt;
int pa= object1.packageInt;
object1.publicPrint();
object1.privatePrint();
object1.packagePrint();
System.exit(0);
}
}
// Which statements will not compile?
5
Package Test Class 2
package Lecture11TestPkg;
import Lecture11PkgClass.*;
// Different package (or none)
// Import desired package
public class PkgTest {
public static void main(String[] args) {
PkgClass object1= new PkgClass(1, 2, 3);
int pu= object1.publicInt;
//
int pr= object1.privateInt;
No access
//
int pa= object1.packageInt;
No access
object1.publicPrint();
//
object1.privatePrint();
No access
//
object1.packagePrint();
No access
System.exit(0);
}
}
Searching packages
• Java® always looks in the default
package for classes
• searching imported packages of form
java.util.Vector• A classes's full name can be used, as
in
Vector v = new java.util.Vector();
6
Inner classes
• Java® allows you to define classes
within another class
• An inner class is only defined within
its outer class
• Inner classes cannot have static
members
Inner class example
public class TopLevelClass {
private int x;
// more code here
class InnerClass {
private int y;
y=x
// more code here
}
}
7
public class TopLevelClass {
private int x=4;
public static void main(String[ ] arg) {
new TopLevelClass();}
public TopLevelClass() {
InnerClass inst1 = new InnerClass();
System.out.println("Outer = "+x+ "Inner= " +
inst1.y);
x = 5;
InnerClass inst2 = new InnerClass();
System.out.println("Outer = "+x+ "Inner = "
inst2.y);
}
private class InnerClass {
private int y;
public InnerClass() {
y = x*x; }
}
}
Uses for inner classes
• Sometimes used for very specialized
classes that aren't intended for reuse
• Example: Items that are intended to
be used on a list, where each item is
an instance of an inner class.
Java® is a trademark or registered trademark of Sun Microsystems, Inc. in the United
States and other countries.
8
Download