CIS-260 Home http://www.c-jump.com/bcc/
Recall that class is a blueprint of an object.
Therefore, multiple instances of an object of the same class can be instantiated.
Each instance occupies its own memory
Each instance has its own identity.
However, we also might need memory that is shared by all objects.
State of an object is determined by current values stored in the data attributes
The attributes can have the following levels of visibility:
Local attributes
Object attributes
Class attributes
Scope of an attribute is visibility combined with the type of memory allocation of the attribute.
Local attributes are invisible outside of the scope of an operation.
Scope of operation is defined by execution entering /exiting the operation.
Lifetime of local attribute is limited to the duration of a call.
In Java and C++ scope is delineated by pairs of matching curly braces: { }
// Java code: public class Number { public void method1() { int count; } public void method2() { } }
|
|
|
|
Instantiate some Numbers:
Number number1 = new Number(); Number number2 = new Number(); Number number3 = new Number();
At this point we have 3 separate copies of the count attribute, because each object allocates its own recourses:
number11.method1(); number12.method2(); number13.method1();
It is possible to take attributes outside of instance scope.
This is done by declaring attributes static:
public class Number { private static int count; public void method1() { } }
In this arrangement each instance of the Number object will share a single copy of the count attribute.