public foo{
    public int a=6;
    public void method(){
        System.out.println(a);
    }
}

This code is defining a attribute which will be made available in all methods. As it has the public modifier it is also available to other classes. In order to use it, like methods, you need to create a object of the class. This is why it is known as object scope.

foo myObject = new foo();
foo secObject = new foo();
myObject.a = 5;
myObject.method();
secObject a = 10;
secObject.method();
myObject.method();

With two objects created they both have their own version of a. myObject was assigned the value of 5 and secObject the value of 10. Java will keep these seperate. Later, when you look at references in more detail, you will be able to see why Java displays this behaviour.

next class scope >>

 
Basics
Menu
Search