Professional Documents
Culture Documents
00 Lecture 11
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 theyre 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
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; its the default with no keyword
Protected: Used with inheritance (covered later)
Like a private variable, except its 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 Fortes 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;
4
Package Test Class 1
package Lecture11PkgClass; // In same package
5
Package Test Class 2
package Lecture11TestPkg; // Different package (or none)
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
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);
}