CIS-260 Home http://www.c-jump.com/bcc/

Data Attribute Scope and Visibility


  1. Data Attribute Scope and Visibility
  2. Attribute Visibility
  3. Local Attributes
  4. Method scope and attribute names
  5. Object Attributes
  6. Number class example
  7. Class-level Attributes

1. Data Attribute Scope and Visibility


2. Attribute Visibility


3. Local Attributes


4. Method scope and attribute names

  • Consider:

    
    public class Number {
        public void method1() {
            int count;
        }
        public void method2() {
            int count;
        }
    }
    
    
  • The code in this example is legal and correct.

  • Actual names of integer variables inside method1 and method2 are unimportant, because the same thread of execution can enter either of methods, but not both at the same time.

    • Note: the above is true if method1 invokes method2 or vice versa.

5. Object Attributes

  • Some attributes should be shared by all operations of our object. For example,

    
    public class Number {
        // available to all methods:
        private int count;
        public void method1() {
            count = 1;
        }
        public void method2() {
            count = 2;
        }
    }
    
    
  • Here, attribute count is declared within the scope of the Number class.

  • This means that now attribute count and both methods are declared in the same scope, at the class level.

  • Consequently, both methods will have access to the count attribute.

  • Each Number object will have its own copy of the count attribute.

  • Both methods are modifying the same attribute in memory by setting count to a specific value.

6. Number class example


7. Class-level Attributes