- Forward


Creating and Using Objects
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • Class:
    • A definition of a set that is written in terms of the properties of the elements/members (i.e., an intensive definition of a set)
    • The set of elements/members so defined
  • Object:
    • An instance of a class (i.e., an element of the set)
  • Naming Conventions in Java:
    • Class identifiers begin with am uppercase letter
    • Object identifiers 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 and evaluates to a reference)
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
  • The Membership Operator:
    • A binary operator with an object or class as the left-operand and a member as the right-operand
  • Using the Membership Operator with Accessible Instance Attributes:
    • object.attribute
  • Using the Membership Operator with Accessible Instance Behaviors:
    • object.method([actualparameter][,actualparameter]...)
    • This is referred to as calling or invoking a method
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 java.awt.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
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 Revisited
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 Revisited (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
 
Using static Attributes
Back SMYC Forward
  • Definition:
    • Attributes that belong to a class rather than instances of a class
  • Implication:
    • They can be used without instantiating an object
  • Common Uses:
    • Constants, flags, special values, etc...
Some Useful static Attributes
Back SMYC Forward
  • In the Integer Class:
    • MIN_VALUE
    • MAX_VALUE
  • In the Double Class:
    • NEGATIVE_INFINITY
    • POSITIVE_INFINITY
    • MIN_VALUE
    • MAX_VALUE
  • In the Math Class:
    • E (base of the natural log)
    • PI
Using static Methods
Back SMYC Forward
  • Definition:
    • Methods that belong to a class rather than instances of a class
  • Implication:
    • They can be used without instantiating an object
  • Common Uses:
    • Utilities, mathematical functions
Some Useful static Methods
Back SMYC Forward
  • In the System Class:
    • currentTimeMillis()
    • exit()
  • In the Math Class (A Utility Class):
    • abs()
    • sin()
    • cos()
    • tan()
    • log()
    • pow()
    • sqrt()
Some Useful static Methods (cont.)
Back SMYC Forward
  • In the Integer Class:
    • parseInt()
    • toString()
  • In the Double Class:
    • parseDouble()
    • toString()
Java vs. Python - Important Differences
Back SMYC Forward
  • The new Operator:
    • In Python, the constructor is invoked directly (e.g., bullwinkle = Moose())
  • Primitive Types:
    • In Python, there are no primitive types (just objects/references)
There's Always More to Learn
Back -