CH 2
CH 2
long 8 bytes -9, 223, 372, 036, 854, 775, 9, 223, 372, 036, 854, 775, 807
808
• Here, b1 has been set to null, but b2 still points to the original
object.
CONSTRUCTORS
• A constructor initializes an object immediately upon
creation.
• It has the same name as the class in which it resides and is
syntactically similar to a method.
• Once defined, the constructor is automatically called
immediately after the object is created, before the new
operator completes.
• Constructors look a little strange because they have no
return type, not even void.
• This is because the implicit return type of a class’
constructor is the class type itself.
• It is the constructor’s job to initialize the internal state of
an object so that the code creating an instance will have a
fully initialized, usable object immediately.
TYPES OF CONSTRUCTORS
• Copy Constructor
• Parameterized Constructor
• Default Constructor
INSTANCE VARIABLE HIDING
• It is illegal in Java to declare two local variables with the same name inside the same
or enclosing scopes. You can have local variables, including formal parameters to
methods, which overlap with the names of the class’ instance variables.
• When a local variable has the same name as an instance variable, the local variable
hides the instance variable.
• This is why width, height, and depth were not used as the names of the parameters
to the Box( ) constructor inside the Box class.
• If they had been, then width would have referred to the formal parameter, hiding
the instance variable width.
• It is usually easier to simply use different names, there is another way around this
situation.
• Because this lets you refer directly to the object, you can use it to resolve any name
space collisions that might occur between instance variables and local variables.
Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}
THE finalize( ) METHOD
• If an object is holding some non-Java resource such as a file handle or window character
font, then you might want to make sure these resources are freed before an object is
destroyed.
• By using finalization, you can define specific actions that will occur when an object is just
about to be reclaimed by the garbage collector.
• The Java run time calls the finalize( ) method whenever it is about to recycle an object of
that class.
• The garbage collector runs periodically, checking for objects that are no longer
referenced by any running state or indirectly through other referenced objects. Right
before an asset is freed, the Java run time calls the finalize( ) method on the object.
protected void finalize( )
{ // finalization code here }
• Te keyword protected is a specifier that prevents access to finalize( ) by code defined
outside its class.
• It is important to understand that finalize( ) is only called just prior to garbage collection,
not when an object goes out-of-scope. So you cannot know when—or even if—finalize( )
will be executed.
• Your program should provide other means of releasing system resources, etc., used by
the object.
• It must not rely on finalize( ) for normal program operation.
ACCESS CONTROL
• Java’s access specifiers are public, private, and protected. Java
also defines a default access level.
• protected applies only when inheritance is involved.
• When a member of a class is modified by the public specifier,
then that member can be accessed by any other code.
• When a member of a class is specified as private, then that
member can only be accessed by other members of its class.
• Now you can understand why main( ) has always been
preceded by the public specifier. It is called by code that is
outside the program—that is, by the Java run-time system.
• When no access specifier is used, then by default the member
of a class is public within its own package, but cannot be
accessed outside of its package.
• NOTE: Check the programs explained in class for overloading
and method arguments and return type.
ACCESS CONTROL
THE Object CLASS
• There is one special class, Object, defined by Java.
• All other classes are subclasses of Object.
• That is, Object is a superclass of all other classes.
• This means that a reference variable of type
Object can refer to an object of any other class.
• Also, since arrays are implemented as classes, a
variable of type Object can also refer to any array.
• Object defines the many methods, which means
that they are available in every object.
• The methods getClass( ), notify( ), notifyAll( ), and
wait( ) are declared as final. You may override the
others.
THE Object CLASS