0% found this document useful (0 votes)
42 views28 pages

Classes and Other Concepts

This document discusses various concepts related to classes and objects in Java, including: - The "this" reference, which allows objects to access their own instance variables and methods. - Static class members, which are shared by all objects of a class rather than belonging to individual objects. - Garbage collection in Java, which automatically reclaims memory from objects no longer referenced. - The "final" keyword, which makes variables unmodifiable constants.

Uploaded by

pilotbairexd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views28 pages

Classes and Other Concepts

This document discusses various concepts related to classes and objects in Java, including: - The "this" reference, which allows objects to access their own instance variables and methods. - Static class members, which are shared by all objects of a class rather than belonging to individual objects. - Garbage collection in Java, which automatically reclaims memory from objects no longer referenced. - The "final" keyword, which makes variables unmodifiable constants.

Uploaded by

pilotbairexd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

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
8.4 Referring to the Current Object’s Members with
the this Reference
8.10 Garbage Collection
8.11 static Class Members
7.2.5 Additional Notes on This Example (Notes on
static Methods)
8.12 static Import
8.13 final Instance Variables
8.14 Package Access

©1992-2015 by Pearson Education, Inc.


All Rights Reserved.
8.4 Referring to the Current Object’s
Members with the this Reference
 Every object can access a reference to itself with
keyword this.
 Can use the this reference implicitly and 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.
 Enables the class’s code to know which object should be
manipulated.
 Can also use keyword this explicitly in an instance
method’s body.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
8.4 Referring to the Current Object’s
Members with the this Reference (Cont.)
 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.
 When one source-code (.java) file contains multiple

class declarations, the compiler places both class files


for those classes 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.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
8.4 Referring to the Current Object’s
Members with the this Reference (Cont.)
 SimpleTime declares three private instance variables
—hour, minute and second.
 If parameter names for the constructor that are identical to
the class’s instance-variable names.
 We use the this reference to refer to the instance
variables.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
8.11 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.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
8.11 static Class Members (Cont.)
 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.
 static class members are available as soon as the
class is loaded into memory at execution time.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
8.11 static Class Members (Cont.)
 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.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
7.2.5 Additional Notes on This Example
Notes on static Methods
 A static method can call other static methods

of the same class directly (i.e., using the method


name by itself) and can manipulate static
variables in the same class directly.
 To access the class’s instance variables and instance

methods, a static method must use a reference to


an object of the class.
 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.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
8.11 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.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
8.12 static Import
 A static import declaration enables you to import
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
 single static import: imports a particular static member
 static import on demand: imports all static members of
a class

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
8.12 static Import (Cont.)
 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.

 static import declarations import only static class


members.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
8.13 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 is an error.
private final int INCREMENT;
 Declares a final (constant) instance variable INCREMENT of type
int.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
8.13 final Instance Variables (cont.)
 final variables can be initialized when they are
declared or 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.


 A final variable cannot be modified by assignment

after it’s initialized.


 If a final variable is not initialized, 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.
8.10 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.
8.10 Garbage Collection (Cont.)
A Note 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