Classes and Objects Introduction Life Cycle Creation Example
Introduction Objects are created from classes An objects type is its class “is a” versus “has a” relationships Examples
Life Cycle Creation Use Destruction
Creation Object is instance of a class Declaration, Instantiation, Initialization Rectangle rect = new Rectangle(); Type is class. Object reference. new allocates memory (instantiates) Constructor initializes object Default constructor takes no arguments
Using Manipulate or inspect an object’s variables objectReference.variable Use an object’s methods to access variables objectReference.methodName(argumentList)
Destroy Garbage Collector that frees memory used by no longer needed objects. A mark-sweep garbage collector –Scans dynamic memory for object references –Unreferenced objects cleaned up –own thread finalize()
Details (1) Variables and methods are members constructors are not members Can overload constructors super() class constructor this.keyWord
Access to Classes private: No other class can instantiate this class public: Any class can instantiate this class protected: Only subclasses can instantiate package: only classes in the same package can instantiate
Member Variable Declarations static: Class member rather than instance member final: Value cannot be changed (constant) transient: non-Serializable volatile: For optimization
Details (2) return : can return object of subclass Overloaded methods distinguished by number and type of arguments Comma delimited set of arguments of any valid Java data type. this, super Local Variables
Pass by Value Arguments passed by value not reference cannot change value of primitive types Access values not actual variables Reference types passed by reference Reference a Kind of Pointer No Pointer to Anonymous Memory Value of reference types are the reference
... int r = -1, g = -1, b = -1; pen.getRGBColor(r, g, b); System.out.println("red = " + r + ", green = " + g + ", blue = " + b);... class Pen { int redValue, greenValue, blueValue; void getRGBColor(int red, int green, int blue) { // red, green, and blue have been created // and their values are }
class Pen { int redValue, greenValue, blueValue;... // this method does not work as intended void getRGBColor(int red, int green, int blue) { red = redValue; green = greenValue; blue = blueValue; }... int r = -1, g = -1, b = -1; pen.getRGBColor(r, g, b); System.out.println("red = " + r + ", green = " + g + ", blue = " + b);...
class RGBColor { public int red, green, blue; } class Pen { int redValue, greenValue, blueValue; void getRGBColor(RGBColor aColor) { aColor.red = redValue; aColor.green = greenValue; aColor.blue = blueValue; }
... RGBColor penColor = new RGBColor(); pen.getRGBColor(penColor); System.out.println("red = " + penColor.red + ", green = " + penColor.green + ", blue = " + penColor.blue);...
Access
Instance and Class Members Instance is default Each new object gets new copy of each of classes instance variables A single copy is created of a class member (static). Accessible from class itself. System.out.println(), Integer.parseInt(String s) Class methods can only operate on class variables Class members for constants and efficiency
class AnIntegerNamedX { int x; public int x() { return x; } public void setX(int newX) { x = newX; }... AnIntegerNamedX myX = new AnIntegerNamedX(); AnIntegerNamedX anotherX = new AnIntegerNamedX(); myX.setX(1); anotherX.x = 2; System.out.println("myX.x = " + myX.x()); System.out.println("anotherX.x = " + anotherX.x());...
myX.x = 1 anotherX.x = 2 class AnIntegerNamedX { static int x; public int x() { return x; } public void setX(int newX) { x = newX; } myX.x = 2 anotherX.x = 2
class AnIntegerNamedX { int x; static public int x() { return x; } static public void setX(int newX) { x = newX; } AnIntegerNamedX.java:4: Can't make a static reference to nonstatic variable x in class AnIntegerNamedX. return x; ^
class AnIntegerNamedX { static int x; static public int x() { return x; } static public void setX(int newX) { x = newX; } myX.x = 2 anotherX.x = 2