Aqua Phoenix
     >>  Lectures >>  Java 2  
 

Navigator
   
 
       
   

2.1 Objects

All functionality and behavior of a program is embedded into objects in Object Oriented Programming. Objects necessarily have to be able to communicate with and call on each other in order to tie together all of the pieces. These ties are created through members of classes that can be accessed from outside of the classes. Public and protected variables and methods, including constructors, are such members. Members of a class are accessed by naming the object and the member and separating the two with a dot: object.member refers to the member of the object.

Without referring to their detailed definitions, here are some examples of how members of classes are accessed:

Access a variable by the name of age in object someObject.

someObject.age
Access a method by the name getSomething in object someObject, without passing arguments.

someObject.getSomething()
Access a method by the name putSomething in object someObject, and pass a parameter by the name of someInt.

someObject.putSomething(int someInt)
Access an object by the name of someOtherObject in object someObject. Here, someOtherObject is defined as a protected or public variable:

someObject.someOtherObject
Access an object in object in object someObject by means of a method that returns the object. This typically means that the variable is declared as private, and can only be accessed through methods:

someObject.getTheOtherObject()
Access an object by the name of someOtherObject, and access one of its member variables:

someObject.someOtherObject.yetAnotherObject
Access an object by means of a method, and access one of its member variables:

someObject.getTheOtherObject().yetAnotherObject
An example of such a construct which we have already been using is:

System.out.println(...)
Here, System refers to a class. out refers to a static variable. It is static because there was no need to create an instance of class System. println(...) refers to a method in class out. This method also is static, because out has not been instantiated as an object.

There is virtually no limit to the number of nested objects a particular statement can traverse, thus the following statement is certainly valid, if the methods or variables are actually defined in the given objects:

a.b().c().d().e().f().g().h.i.j.k.l.m.n().o().p.q()