3 Classes and Other Concepts

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Classes and other Concepts

Java™ How to Program, 10/e


Late Objects Version

© Copyright 1992-2015 by Pearson Education, Inc. All Rights


Reserved.
References & Reading
 The content is mainly selected (sometimes modified)
from the original slides provided by the authors of the
textbook

 Readings
 Chapter 7: Introduction to Classes and Objects
 Chapter 8: Classes and Objects: A Deeper Look

©1992-2015 by Pearson Education, Inc.


All Rights Reserved.
Outline
 Referring to the Current Object’s Members with the this Reference
 Garbage Collection
 static Class Members
 Additional Notes on This Example (Notes on static Methods)
 static Import
 final Instance Variables
 Package Access

©1992-2015 by Pearson Education, Inc.


All Rights Reserved.
Referring to the Current Object’s Members using this keyword

 Every object can access a reference to itself with keyword this.


 Commonly, this reference keyword can be used implicitly or explicitly.
 When an instance method is called for a particular object, the method’s body implicitly
uses keyword this to refer to the object’s instance variables and other methods.
 We can also use keyword this explicitly in an instance method’s body.
 When you compile a .java file containing more than one class, the compiler
produces a separate class file with the .class extension for every compiled class and
put them in the same directory.
 A source-code file can contain only one public class—otherwise, a compilation
error occurs.
 Non-public classes can be used only by other classes in the same package.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 Notes:

◦ SimpleTime declares three private instance variables— hour, minute and


second.

◦ If parameter names for the constructor are identical to the class’s instance-variable names
then this reference is used to refer to the instance variables.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
static Class Members
 In certain cases, only one copy of a particular variable should be shared by all
objects of a class. A static field—called a class variable— is used in such cases.
 A static variable represents classwide information—all objects of the class
share the same piece of data.
 The declaration of a static variable begins with the keyword static.
 Static variables have class scope —they can be used in all of the class’s
methods.
 To access a public static member (variable or method) when no objects of
the class exist (and even when they do), prefix the class name and a dot (.) to the
static member, as in Math.PI.

 Can access a class’s public static members through a reference to any object
of the class

 private static class members can be accessed by client code only through
methods of the class.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
static Class Members (Cont.)
 static class members are available as soon as the class is loaded into memory at
execution time.
 A static method cannot access a class’s instance variables and instance
methods, because a static method can be called even when no objects of the
class have been instantiated.
 For the same reason, the this reference cannot be used in a static method.
 The this reference must refer to a specific object of the class, and when a
static method is called, there might not be any objects of its class in memory.

 If a static variable is not initialized, the compiler assigns it a default value— 0


in case of variable of type int.
 A static method can (1) call other static methods of the same class directly (i.e.,
using the method name by itself) and (2) can manipulate static variables in the
same class directly.
 Instance methods can access all fields (static variables and instance
variables) and methods of the class.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
static Class Members (Cont.)

 String objects in Java are immutable—they cannot be modified after they are
created.
 Therefore, it’s safe to have many references to one String object.
 This is not normally the case for objects of most other classes in Java.
 If String objects are immutable, you might wonder why are we able to use
operators + and += to concatenate String objects.
 String-concatenation actually results in a new String object containing the
concatenated values—the original String objects are not modified.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
static Import
 A static import declaration enables you to import only the static members of
a class or interface so you can access them via their unqualified names in your
class— that is, the class name and a dot (.) are not required when using an
imported static member.

 Two forms of importing static members:


 single static import: imports a particular static member
 static import on demand: imports all static members of a class.

 The following syntax imports a particular static member:


import static packageName.ClassName.staticMemberName;

 The following syntax imports all static members of a class:


import static packageName.ClassName.*;

 * indicates that all static members of the specified class should be available
for use in the class(es) declared in the file.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
final Instance Variables
 The principle of least privilege is fundamental to good software engineering.
 Code should be granted only the amount of privilege and access that it needs to accomplish
its designated task, but no more.
 Makes your programs more robust by preventing code from accidentally (or maliciously)
modifying variable values and calling methods that should not be accessible.

 Keyword final specifies that a variable is not modifiable (i.e., it’s a constant) and any attempt
to modify it by assignment after it’s initialized is an error.
EX:
private final int INCREMENT;
// Declares a final (constant) instance variable INCREMENT of type int.

 final variables can be initialized when (1) they are declared or (2) by each of the class’s
constructors so that : each object of the class has a different value.
 If a class provides multiple constructors, every one would be required to initialize each final
variable.
 If a final variable is not initialized, then a compilation error occurs.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
Self-Reading

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
Garbage Collection
 Every object uses system resources, such as memory.
 Need a disciplined way to give resources back to the system when they’re no
longer needed; otherwise, “resource leaks” might occur.

 The JVM performs automatic garbage collection to reclaim the memory occupied
by objects that are no longer used.
 When there are no more references to an object, the object is eligible to be
collected.
 Collection typically occurs when the JVM executes its garbage collector, which
may not happen for a while, or even at all before a program terminates.

 Memory leaks that are common in other languages like C and C++ (because
memory is not automatically reclaimed in those languages) are less likely in Java,
but some can still happen in subtle ways.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
Garbage Collection (Cont.)

Notes about Class Object’s finalize Method:

 Every class in Java has the methods of class Object (package java.lang), one
of which is method finalize.

 You should never use method finalize, because it can cause many problems.

 The original intent of finalize was to allow the garbage collector to perform
termination housekeeping on an object just before reclaiming the object’s memory.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.

You might also like