Java Reference
In-Depth Information
garbage collector takes to call an object's
finalize()
method is not standard in all
implementations of the Java interpreter. This could take place long after the last reference
to the object was removed. In most cases, you don't need to use
finalize()
at all.
After finishing today's lesson, you should have a pretty good idea of the relationship
among classes in Java and programs you create using the language.
Everything you create in Java involves the use of a main class that interacts with other
classes as needed. It's a different programming mindset than you might be used to with
other languages.
Today, you put together everything you have learned about creating Java classes. Each of
the following topics was covered:
Instance and class variables, which hold the attributes of a class and objects created
from it.
n
Instance and class methods, which define the behavior of a class. You learned how
to define methods—including the parts of a method signature, how to return values
from a method, how arguments are passed to methods, and how to use the
this
keyword to refer to the current object.
n
The
main()
method of Java applications and how to pass arguments to it from the
command line.
n
Overloaded methods, which reuse a method name by giving it different arguments.
n
5
Constructor methods, which define the initial variables and other starting condi-
tions of an object.
n
Q In my class, I have an instance variable called
origin
. I also have a local vari-
able called
origin
in a method, which, because of variable scope, gets hidden
by the local variable. Is there any way to access the instance variable's value?
A
The easiest way to avoid this problem is to give your local variables the same
names that your instance variables have. Otherwise, you can use
this.origin
to
refer to the instance variable and
origin
to refer to the local variable.