- Forward


Using Members in Classes
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • Creating Classes:
    • Writing an intensive definition of a set (i.e., a definition that is written in terms of the properties of the elements)
  • Using Objects:
    • Establish an identity (in a declaration statement)
    • Allocate memory and initialize the attributes (using the new operator)
    • Invoke methods on the object (using the . operator with the object identifier as the right-side operand)
  • Using Classes:
    • Use accessible class attributes or class methods (using the . operator with the class identifier as the right-side operand)
The Owning Object
Back SMYC Forward
  • An Observation:
    • The . operator requires an object or class identifier as the left-side operand
  • A Question:
    • What should the left-side operand be when writing the class (i.e., inside of the class definition)?
  • The Answer:
    • this (which is a reference to the owning object)
An Example - Attributes and a Constructor
Back SMYC Forward
javaexamples/oopbasics/pf_classes/PictureFrame.java (Fragment: 0)
 

Why are some attributes static and some not?
What are the formal parameters of the constructor?
What local variables are used in the constructor?
What methods are invoked in the constructor and are they static or not?

An Example (cont.) - Getters
Back SMYC Forward
javaexamples/oopbasics/pf_classes/PictureFrame.java (Fragment: Getters)
 

How do you refer to the owning object?
Why isn't it necessary to do so?
Could you do so anyway?

An Example (cont.) - Private Methods
Back SMYC Forward
javaexamples/oopbasics/pf_classes/PictureFrame.java (Fragment: PrivateMethods)
 

Which new methods are private?
What is true of private members?
Why include private methods?
Is it necessary to use this. when invoking private methods?
Which members are static and which are not?

An Example (cont.) - Comparing Objects
Back SMYC Forward
javaexamples/oopbasics/pf_classes/PictureFrame.java (Fragment: Equals)
 

How many objects are being compared?
How are they referred to?
Is it necessary to use this.?
Why are the attributes of other accessible?

An Example (cont.) - Getting a String Representation
Back SMYC Forward
javaexamples/oopbasics/pf_classes/PictureFrame.java (Fragment: ToString)
 

When might this method be useful?
What operators are being used?

Using a Non-Static Member in a Static Context
Back SMYC Forward
  • Three Related Questions:
    • What happens if you use a non-static member in a static method, why, and when?
    • public class Interval { private double left, right; public Interval(double left, double right) { this.left = left; this.right = right; } // What's happens here, why, and when? public static double getLength() { return this.right - this.left; } }
  • Another Question:
    • How do you fix this problem? (Hint: The answer is not to behave like 10,000 monkeys at 10,000 keyboards!)
Using a Non-Static Member in a Static Context(cont.)
Back SMYC Forward
  • Where Else Beginning Programmers Often Encounter This:
    • In the main() method of the main class
    • public class Game { private int lives; // What's happens here and why? (And when?) public static void main(String[] args) { lives = Integer.parseInt(args[0]); } }
  • Another Question:
    • How do you fix this problem? (Hint: Again, the answer is not to behave like 10,000 monkeys at 10,000 keyboards!)
Using a Static Member with an Object on the Left
Back SMYC Forward
  • Is It Possible?
    • Yes, because static members belong to the class (i.e., the set) and hence to individual elements
  • Is It A Good Idea?
    • No, it's confusing and there is no advantage to doing so
Java vs. Python - Important Differences
Back SMYC Forward
  • The Owning Object:
    • In Python, the owning object is often named self (though needn't be) and is the first parameter passed to a method
  • Class and Static Methods:
    • Python makes a disctinction between class methods and static methods
There's Always More to Learn
Back -