
Object-oriented programming (OOP)
Object-oriented programming is an inherent part of Java. It is not possible to code Java without
understanding some basic object concepts. How to create and use objects is a very important topic. OOP is
not a topic you master in five days; it takes significant time to master all the techniques to write easily
extended and reusable code. However, getting started on OOP is actually quite easy, and you may find it
more intuitive than other techniques like structured programming.
In this chapter, you start using Java objects and get used to some of the more commonly used Java objects
like String. You also learn how to create your own object types called classes and make objects from those
definitions. Objects and classes are the foundation of the Java programming language and the building blocks
of any potential application.
Object-Oriented Programming
Java is an object-oriented programming language. Object-oriented programming represents a way of creating
code. The created code mimics real world entities or “objects.” Objects are software bundles of data and
related procedures. For example, a bank account object would contain data such as its balance, account
number, and owner information. A bank account object would also contain procedures to open, debit, credit,
apply interest, and close an account.
The result of mimicking the real world is code that more aptly matches the problem the programmer is trying
to solve. This process of writing code to be like the real world is called abstraction.
Most programmers see object-oriented programming as an evolution of coding practice. Initially all coding
was done in machine-level language. After machine language came low-level languages, such as assembly
language. In assembly language, code constructs mimicked the machine behind the code. Second-generation
languages such as FORTRAN, Pascal, and C made coding easier by adding some reusable structures. In fact,
these types of programming languages are referred to as structured programming languages. They also
introduced libraries of functionality. C++, Smalltalk, and other languages came on the scene later, adding
object-oriented principles to software development.
OO has several distinct advantages, if done properly. OO is more extensible, allowing you to add things to
your application faster and easier than with structured code. OO is easier to maintain. OO provides better
organization for code reuse and library functionality.
Objects vs. Primitives
In Java, most of the time you deal with objects. However, you also use primitive data types. There are eight
primitive data types: byte, short, int, long, float, double, boolean and char.
Everything else in Java is an object!
More information on primitive data types (or primitives for short) is provided later. Primitives behave
differently in that they don’t have data and procedures. Also, the JVM treats primitives differently.
Creating (Instantiating) Objects
Using objects is fundamental to using and writing Java. The following code examples create three different
types of Java objects.
String s = new String();
Customer c = new Customer();
Circle circle = new Circle();
A few definitions are needed to have conversations around this code.
• Reference type — Defines to what type of object the reference points. (Is it going to be a primitive
or an object? If it is an object, what type of object will it be?)
• Reference variable — Place holder for an object (or primitive).
• The “new” keyword — Creates an object (called an instance) of the object type.
• Object type — The actual type of object created in memory.
Notice that primitives do not use the “new” keyword or object type. They are simply assigned a value.
int i = 5;
char c = 'a';
double pi = 3.14;
The technical term for creating an object is instantiation.
Another important note about creating objects is to understand when the object is actually created. When a
reference is defined, this does not create the object. Defining a reference only means that it is ready to point
to an object. The object is created when “new” is used.
Circle c; //No object exists yet
c = new Circle(); //Object created and assigned to 'c'
One reason it is necessary to understand this is because a single reference can be reused over and over again. A
new object can be assigned to an existing reference.
Customer c = new Customer("CID10394");
c = new Customer("CID23511"); //Same reference – different objects
c = new Customer("CID39203");
c = new Customer("CID88374");
Depending on other code in your application, the old object that your reference was pointing to may be
destroyed. Destroying the object sounds bad, but this is usually a good thing. This means that objects that are
no longer needed are cleaned out of memory. The destruction and cleanup of objects is discussed later.
Object Oriented Programming in Java
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services