SCOPE - PowerPoint PPT Presentation

About This Presentation
Title:

SCOPE

Description:

Declarations and statements can be grouped into a block using braces ... Members of the class in which they are declared. Not part of any instance of the class ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 23
Provided by: thaddeusf
Category:

less

Transcript and Presenter's Notes

Title: SCOPE


1
SCOPE I/O
  • CSC 171 FALL 2004
  • LECTURE 5

2
CSC171
  • Room Change
  • Thursday, September 23.
  • CSB 209
  • THERE WILL BE A (group) QUIZ!
  • - topic the CS department at UR

3
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

4
Class Scope
  • Within a class definition reference variables of
    this classs type can be used to access all
    members regardless of their accessibility
    modifiers

5
  • 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)

6
  • public static Light duplicateLight (Light
    oldLight)
  • Light newLight new Light()
  • newLight.noOfWatts oldLight.noOfWatts
  • newLight.indicator oldLight.indicator
  • newLight.location
  • new String(oldLight.location)
  • return newLight
  • // end of class

7
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

8
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

9
  • 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

10
Member Accessibility Modifiers
  • Accessibility modifiers help a class define its
    relationship to its client members
  • Accessibility of members can be one of
  • public
  • protected
  • default (a.k.a package)
  • private



-
11
public Members
  • Least restrictive
  • Accessible everywhere
  • Both class (static) and instance members

12
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

13
default Members
  • When no access modifier is specified for a member
  • It is only accessible by another class in the
    same package where its class is defined
  • Even if a class is visible in another (possibly
    nested) package, the member is not accessible.

14
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

15
Other Modifiers for members
  • static
  • final
  • abstract
  • synchonized
  • native
  • transient
  • volitile

16
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

17
final
  • A final variable is a constant
  • Its value cannot be changed once initialized
  • Applies to instance, static, and local vars
  • Final methods cannot be overridden (redefined in
    subclasses)

18
abstract (incomplete)
  • An abstract method has the following syntax
  • abstract ltreturn typegt ltnamegt (ltparamsgt)
  • 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

19
advanced modifiers
  • synchronized used for multi-threaded code
  • native used for foreign (C,C) methods
  • transient if you store it, you dont save the
    values
  • volatile some other thread could change its
    value

20
IO in JAVA
  • If there is System.out
  • There must be System.in
  • And other methods
  • Like pop ups

21
  • import javax.swing.JOptionPane
  • public class Popup
  • public static void main(String args)
  • String input1 JOptionPane.showInputDialog("Fi
    rst number")
  • String input2 JOptionPane.showInputDialog("Se
    cond 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)

22
  • 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(isre
    ader)
  • String input1 console.readLine()
  • System.out.println(input1)
Write a Comment
User Comments (0)
About PowerShow.com