1 / 22

Java Scope and Accessibility Explained

Understand the lifetime and accessibility rules of variables in Java, including class scope, block scope, and member accessibility modifiers. Learn about static, final, abstract, synchronized, native, transient, and volatile modifiers, as well as I/O in Java.

tmoyers
Download Presentation

Java Scope and Accessibility Explained

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 class’s 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. parameter Local variable 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 parameteroldLight local variablenewLight 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 it’s 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 it’s 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 • It’s 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 <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

  19. “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

  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("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); } }

  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(isreader); String input1 = console.readLine(); System.out.println(input1); } }

More Related