Java Reference
In-Depth Information
Summary
If today was your first exposure to object-oriented programming, it probably seems theo-
retical and a bit overwhelming.
1
When your brain has just been stuffed with object-oriented programming concepts and
terminology for the first time, you might be worried that there's no room left for the Java
lessons of the remaining 20 days.
At this point, you should have a basic understanding of classes, objects, attributes, and
behavior. You also should be familiar with instance variables and methods. You'll be
using these right away tomorrow.
The other aspects of object-oriented programming, such as inheritance and packages,
will be covered in more detail on upcoming days. You'll work with object-oriented pro-
gramming in every remaining day of the topic. By the time you finish the first week,
you'll have working experience with objects, classes, inheritance, and all the other
aspects of the methodology.
Q&A
Q In effect, methods are functions defined inside classes. If they look like func-
tions and act like functions, why aren't they called functions?
A Some object-oriented programming languages do call them functions. (C++ calls
them member functions.) Other object-oriented languages differentiate between
functions inside and outside a body of a class or object because in those languages
the use of the separate terms is important to understanding how each function
works. Because the difference is relevant in other languages and because the term
method is now in common use in object-oriented terminology, Java uses the term
as well.
Q What's the distinction between instance variables and methods and their
counterparts, class variables and methods?
A Almost everything you do in a Java program involves instances (also called
objects) rather than classes. However, some behavior and attributes make more
sense if stored in the class itself rather than in the object.
For example, the Math class in the java.lang package includes a class variable
called PI that holds the approximate value of pi. This value does not change, so
there's no reason different objects of that class would need their own individual
copy of the PI variable. On the other hand, every String object contains a method
called length() that reveals the number of characters in that String . This value
can be different for each object of that class, so it must be an instance method.
 
 
Search WWH ::




Custom Search