Application Delivery Fundamentals 2.0:
Java
Module 9: Object-Oriented Programming
Classes and Objects Appendix
Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture.
Mutable and Immutable Data Types: Objects
Mutable
Objects
Immutable
Objects
Contents of the instance can be altered
Contents of the instance cannot be altered
All of the java.lang package wrapper classes are immutable:
Boolean, Byte, Character, Double, Float, Integer, Long,
Short, String.
Copyright © 2012 Accenture All Rights Reserved.
2
Mutable and Immutable Data Types: Example
Consider the code snippet below:
String str1 = “hello”; str1 = “HelloWorld”
Since String is immutable, it is represented in memory in the
following manner:
Heap
hello
HelloWorld
200
100
Stack
str1
Copyright © 2012 Accenture All Rights Reserved.
3
Mutable and Immutable Data Types: String Pool
Consider the code snippet below:
String str1 = “hello”; String str2 = “hello”;
Since the content of both Strings is the same, it will be
represented in memory in the following manner:
Heap
hello
100
100
str1
str2
Stack
Copyright © 2012 Accenture All Rights Reserved.
4
Memory Management: Garbage Collection Special
Case
In special cases, despite Garbage Collection, memory leaks
may still happen in Java.
Programmers need to be aware of such cases and should
use tools to detect and prevent the same.
Copyright © 2012 Accenture All Rights Reserved.
5
OO Concepts: Creating a Package Sample
i
Refer to the sample code:
com.accenture.adf.newcodington.module10.sample. Codington.java
Copyright © 2012 Accenture All Rights Reserved.
6
OO Concepts: Methods Overview
Role of Methods
Methods define the operations that the instances of a class can
perform. These operations form the behavior of objects of the class.
Method Loading
The method code is loaded in the heap and the callstack is on stack.
Copyright © 2012 Accenture All Rights Reserved.
7
OO Concepts: Methods – Calling Method of Another
Class Sample
i
Refer to the sample code:
com.accenture.adf.newcodington.module10.sample. Codington.java
Copyright © 2012 Accenture All Rights Reserved.
8
OO Concepts: Access Control Overview
Hidden details
A class should not expose details of its implementation to other objects
Use of public methods
A class should provide some public methods which can be used to call
object’s implementation.
Adherence to design
Public methods must ensure that modifications made to an object’s state
adhere to the intended design of that object’s class.
Copyright © 2012 Accenture All Rights Reserved.
9
OO Concepts: Access Control – Access Modifiers
Class Access modifiers define how a class can be accessed.
Modifier
Description
(no modifier)
Class can only be accessed from same package.
public
Class can be accessed from anywhere.
Member Access modifiers define how a member (variable or
method) can be accessed.
Modifier
Description
(no modifier)
Member can be accessed within its package only.
public
Member can be accessed from any class of any
package.
protected
Member is accessible in its class package and by its
subclasses.
private
Member is accessible only from its class.
Copyright © 2012 Accenture All Rights Reserved.
10
OO Concepts: Member Variables – Example
Class Event
{
String eventId;
//This variable stores a unique id assigned to each event.
}
Copyright © 2012 Accenture All Rights Reserved.
11