class members

Download Report

Transcript class members

SCOPE & I/O
CSC 171 FALL 2004
LECTURE 5
CSC171
Room Change
Thursday, September 23.
CSB 209
THERE WILL BE A (group) QUIZ!
- topic: the CS department at UR
Scope and Accessibility
Scope is the “lifetime” of a variable
when & where does it “live”
Scope rules govern where in the program a
variable is accessible
Most times java provides explicit modifiers to
control scope
In two cases, scope rules govern accessibility
class scope
block scope
Class Scope

Within a class definition reference variables
of this class’s type can be used to access all
members regardless of their accessibility
modifiers
Class Light {
private int noOfWatts;
private String location;
private boolean indicator; // true or false
public void switchOn() {indicator = true;}
public void switchOff() {indicator = false;}
public boolean isOn() {return indicator;}
public static Light duplicate (Light oldLight) {
public static Light duplicateLight (Light oldLight)
parameter
Local variable
{
Light newLight = new Light();
newLight.noOfWatts = oldLight.noOfWatts;
newLight.indicator = oldLight.indicator;
newLight.location =
new String(oldLight.location);
return newLight;
}
}// end of class
Class Scope
Method duplicateLight has
parameter oldLight
local variable newLight
Even though the instance variables of the
class are private, they are accessible though
the references
Block Scope
Declarations and statements can be grouped
into a block using braces {}
The body of a method is a block
Blocks can be nested
General rule
a variable declared in a block is in scope
inside the block in which it is declared but
is not accessible outside of the block
public static void main(String args[]) {
//String args = “ “; // cannot redeclare
char digit;
{ int index = 5 ;
{ int k = 3; }
{ int k = 4; // ok
// int index ; // not ok
}
}
int index = 6 ; // ok
}
Member Accessibility Modifiers
Accessibility modifiers help a class define its
relationship to it’s client members
Accessibility of members can be one of
public “+”
protected “#”
default (a.k.a “package”)
private “-”
public Members

Least restrictive
 Accessible everywhere
 Both class (static) and instance members
protected Members

Accessible in the package containing this
class
 Accessible by all subclases of the class in
any package where this class is visible
 Non-subclasses in other packages cannot
access protected members
 Less restrictive than default accessibility
default Members

When no access modifier is specified for a
member
 It is only accessible by another class in the
same package where it’s class is defined
 Even if a class is visible in another
(possibly nested) package, the member is
not accessible.
private Members

Most restrictive
 Private members are not accessible from
any other class.
 This applies to subclasses
 Whether in the same package or not
 Not to be confused with inheritance
Other Modifiers for members
static
final
abstract
synchonized
native
transient
volitile
static

Members of the class in which they are
declared
 Not part of any instance of the class
 The class need not be instantiated to
reference static members
 Static members a.k.a class members
final

A final variable is a constant
 It’s value cannot be changed once initialized
 Applies to instance, static, and local vars
 Final methods cannot be overridden
(redefined in subclasses)
abstract (incomplete)
An abstract method has the following syntax
abstract <return type> <name> (<params>)
An abstract method does not have an
implementation (no method body)
Only a prototype is provided
No instantiation is allowed
Instantiable subclasses are forced to
implement the method body
“advanced” modifiers
synchronized – used for multi-threaded code
native – used for “foreign” (C,C++) methods
transient – if you store it, you don’t save the
values
volatile – some other thread could change it’s
value
IO in JAVA
If there is “System.out”
 There must be “System.in”


And other methods
– Like “pop ups”
import javax.swing.JOptionPane;
public class Popup {
public static void main(String [] args) {
String input1 = JOptionPane.showInputDialog("First number");
String input2 = JOptionPane.showInputDialog("Second number");
int i1 = Integer.parseInt(input1);
int i2 = Integer.parseInt(input2);
System.out.println(input1 + " + " + input2 + " == " +
(input1 + input2));
JOptionPane.showMessageDialog(null,
input1 + " + " + input2 + " == " + i1 + i2);
System.exit(0);
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException ;
public class Console {
public static void main(String [] args) throws IOException{
InputStreamReader isreader = new
InputStreamReader(System.in);
BufferedReader console = new BufferedReader(isreader);
String input1 = console.readLine();
System.out.println(input1);
}
}