- Forward


Creating and Using Objects
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Key Concepts
Back SMYC Forward
  • Utility Class:
    • A class that contains only static attributes and methods
  • Class:
    • A definition of a set that is written in terms of the characteristics of the elements (rather than by listing the elements)
    • The characteristics include both attributes and behaviors
    • The focus is on important characteristics - others are ignored (this is called abstraction)
  • Object:
    • A particular element (or instance) of the set defined by a class
Interchangeable Terms (Sometimes Also Modified by "Class" or "Instance")
Back SMYC Forward
  • Characteristic:
    • Member
  • Attribute:
    • Field
    • Property
  • Behavior:
    • Function
    • Method
    • Operation
Data Types in Java Revisited
Back SMYC Forward
  • Primitive/Atomic/Fundamental Types:
    • boolean, double, int,...
  • Class Types (One Kind of Reference Type):
    • Contain attributes (that can be of primitive type or reference type) and methods.
Naming Conventions in Java Revisited
Back SMYC Forward
  • Class Names:
    • Begin with an uppercase letter
  • Object Names:
    • Begin with a lowercase letter (except for "constants" which are in all uppercase)
  • Primitive/Fundamental/Atomic Types:
    • Begin with a lowercase letter
  • Primitive/Fundamental/Atomic Variables:
    • Begin with a lowercase letter (except for "constants" which are in all uppercase)
  • Method Names:
    • Begin with a lowercase letter
Instantiating Objects
Back SMYC Forward
  • Establish an Identity:
    • Declare a variable
  • Initialize the Attributes:
    • Allocate memory for the attributes and call the constructor to perform the initialization using the new operator (which is a unary operator that has a constructor as its operand))
Instantiating Objects (cont.)
Back SMYC Forward

An Example

javaexamples/basics/ObjectExample1.java (Fragment: 0)
 
Instantiating Objects (cont.)
Back SMYC Forward
  • Default Constructor:
    • Has no parameters and is used to create an object with default attributes
  • Explicit Value Constructor:
    • Has parameters that are used to initialize the corresponding attributes of the object
Using Objects
Back SMYC Forward
  • Using Accessible Instance Attributes:
    • object.attribute
  • Using Accessible Instance Behaviors (i.e., calling/invoking methods):
    • object.method([parameter]...)
Using Objects (cont.)
Back SMYC Forward

An Example

javaexamples/basics/ObjectExample2.java (Fragment: 0)
 
Using Objects (cont.)
Back SMYC Forward
  • Capabilities of Objects in Existing Classes:
    • Refer to the "javadocs" (e.g., for the Color class)
  • Capabilities of Objects in Classes we Create:
    • We will have the source code
    • We will document the source code so that we can create "javadocs"
Using Objects (cont.)
Back SMYC Forward
  • An Interesting Question:
    • Between when you declare an object and when you allocate memory for it, what reference/address is in the variable?
  • The Answer:
    • A special reference/address that is denoted by null (and is a reference/address that isn't valid)
  • Two Obvious (but Easily Ignored and Often Misunderstood) Issues:
    • There are no methods or attributes at the special reference/address denoted by null
    • So, attempting to use methods or attributes belonging to null will result in a run-time error called a NullPointerException
Using Objects (cont.)
Back SMYC Forward

An Example

javaexamples/basics/NullExample1.java (Fragment: 0)
 
Comparing Objects
Back SMYC Forward
  • Two Possible Questions:
    • Do two identifiers refer to the same object?
    • Do two objects (of the same class) have the same attributes?
  • Answering These Questions:
    • Answering the first question requires an understanding of references and involves the use of the == operator
    • Answering the second question usually involves an invocation of a .equals() method
Mutability
Back SMYC Forward
  • Mutable Objects:
    • Have attributes that can change (e.g., Rectangle and Frame are classes of mutable objects)
  • Immutable Objects:
    • Do not have attributes that can change (e.g., String and Color are both classes of immutable objects)
Mutability (cont.)
Back SMYC Forward
  • What Makes an Object Mutable?
    • public attributes (but they are very dangerous since there is no control over the changes that can be made)
    • Mutator methods (i.e., methods that allow attributes to be changed in a controlled way)
  • Identifying Mutator Methods:
    • They often begin with set (and, hence, are often called setters)
    • They sometimes have other obvious names (e.g., increaseBy())
  • Be Careful:
    • Methods that return an object of the same type are generally not mutators (e.g., the Color class has a darker() method that doesn't change the owning object but returns another Color object that is "darker")
Using Different Objects/Classes in One Application
Back SMYC Forward

An Example

javaexamples/basics/ObjectExample3.java
 
There's Always More to Learn
Back -