Java Reference
In-Depth Information
Summary
This chapter covered the “Declarations, Initialization, and Scoping” section of the SCJP
exam objectives. Topics discussed include declaring variables, methods, classes, nested
classes, interfaces and enums, as well as the initialization and scoping of variables and
objects.
Declaring a variable involves stating the data type and giving the variable a name.
Variables that represent fi elds in a class are automatically initialized to their corresponding
“zero” value during object instantiation. Local variables must be specifi cally initialized.
Make sure you know the rules for declaring a valid identifi er in Java.
Scope refers to that portion of code where a variable can be accessed. There are three
kinds of variables in Java, depending on their scope: instance variables, class variables and
local variables. Instance variables are the nonstatic fi elds of your class. Class variables are
the static fi elds within a class. Local variables are declared within a method.
An array is a contiguous chunk of memory on the heap representing a fi xed-
size collection of values that all have the same data type. Arrays are Object types in
Java instantiated using the new keyword or with an array initializer. Java allows for
multidimensional arrays.
A Java class is defi ned in a .java source fi le and its corresponding compiled bytecode is
in a .class fi le. A class contains instance variables, class variables, methods, constructors,
nested classes, and instance and static initializers. We discussed the events that occur
during the creation of a new object, referred to as the instantiation process, which is
memory allocation, explicit initialization, parent class construction, instance initializers,
then the class constructor executes.
A constructor is a special method within a class that gets invoked during the
instantiation process. Every class has a constructor: the compiler adds a default constructor
if you do not explicitly defi ne one. Use the this keyword to invoke another constructor in
the same class and the super keyword to invoke a parent class constructor.
An instance initializer is a block of code declared in a class that executes for each
new instance of the class. An instance initializer executes immediately after the parent
class constructor fi nishes and before the body of the class constructor executes. A static
initializer is a block of code that executes once when a class is loaded by the class loader.
A method declaration contains an access specifi er, return value, method name,
parameter list, and a throws clause. A method can also be declared static, fi nal, abstract,
native, or synchronized. Use the ellipsis (...) to declare a variable-length argument list.
Classes typically use the JavaBeans naming convention for declaring a property's accessor
and mutator methods. A static method belongs to the class and is invoked using the name
of the class.
Method overloading is when a class contains multiple methods with the same name
but different parameter lists. Method overriding means writing a child class that contains
the same method signature as its parent class. At runtime the child method executes,
not the parent method. Covariant return types allow the overriding method to return a
data type that is a child of the return type in the parent class. A fi nal method cannot be
Search WWH ::




Custom Search