Lesson: More on Objects

Explore general topics related to using objects in Java programs.

Understand additional basic concepts related to using objects in Java programs.


Now we are going to look at some aspects of using objects in your code.

The first topic is access-modifiers. An access-modifier is one of the modifier keywords that can be specified before the word class when defining a class, before the Data Type when defining a variable and before the return Data Type when defining a method. We have been using public for access modifiers thus far. An access-modifier determines what access other classes have to the components of your class. There are several different access-modifier keywords but for robotics projects, public and private are sufficient. For classes, always use public. This means other classes can access your class.

When defining class level fields (recall that fields are the internal variables or ‘properties’ that an object may have), you can use public or private. For fields, public means other classes can access the field directly. Private means the field may only be accessed by code in your class. For robotics, fields can be public or private, but good programming practice says fields should be private, that is, only accessible outside your class through methods.

When defining methods, you can use public or private. For methods, public means other classes can call the method and private means the method can only be called by other methods in the same class. For robotics, methods can be either public or private as you think best. Private methods are those that support your class and don't really make sense to call from other classes. Here is a video about access control and here is more information about access control.


The next topic is object instance lifetime. When you create an object instance with the new keyword, a block of memory for the class variables is allocated in computer memory and a pointer to that memory location is placed in your object variable. The fact that there is a pointer to the instance in memory is recorded (called reference count). Any additional pointers to the same instance increment the reference count. As long as the reference count for an object instance is not zero, the instance will  be maintained in memory. When the reference count goes to zero, no one is using the instance and the JVM marks the memory as garbage and it will eventually be reclaimed by the system. The reference count is decremented whenever a variable pointing to an object instance is deleted (goes out of scope) or the value of the variable is changed to point to some other object instance or no instance all (null).


The next topic is variable scoping. Scoping describes the lifetime of variables you create in your program. When you define variables in a class at the class level, those variables will be allocated memory when a class instance is created with the new keyword. As long as the class instance is in use (reference count not zero), those fields will exist in memory. When a class instance is not being used anymore (marked for garbage collection), all of it's variables will have their memory released as part of that process.

Variables defined in methods (local) only have memory assigned to them when the method is executing. When a method starts, the local variables are allocated memory and retain it while the method runs. When the method ends, the memory is released. When a variable's memory is released because it's containing method or class ends it is called going out of scope.


Our next topic is the this keyword. The this keyword refers to the current instance of the containing object. The this keyword helps resolve ambiguity in naming within a class. In a constructor or method, if you use a variable or parameter name that is the same as a class field, the method local name hides the class field of the same name. The this keyword allows you to access the hidden name. Additionally, when calling a method which has a parameter which is a reference to the instance of the calling class, this allows you to pass the calling instance. Here is an an example of that:

This may seem a strange example but in reality there are many cases where a method in another class needs our own class instance as a parameter. Here is more information about the this keyword.