<<< Method scope and attribute names     Index     Number class example >>>

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.

<<< Method scope and attribute names     Index     Number class example >>>