Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes and Objects Introduction Life Cycle Creation Example.

Similar presentations


Presentation on theme: "Classes and Objects Introduction Life Cycle Creation Example."— Presentation transcript:

1 Classes and Objects Introduction Life Cycle Creation Example

2 Introduction Objects are created from classes An objects type is its class “is a” versus “has a” relationships Examples

3

4

5

6

7 Life Cycle Creation Use Destruction

8 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

9 Using Manipulate or inspect an object’s variables objectReference.variable Use an object’s methods to access variables objectReference.methodName(argumentList)

10 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()

11

12

13 Details (1) Variables and methods are members constructors are not members Can overload constructors super() class constructor this.keyWord

14 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

15

16 Member Variable Declarations static: Class member rather than instance member final: Value cannot be changed (constant) transient: non-Serializable volatile: For optimization

17

18

19 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

20 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

21 ... 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 -1... }

22 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);...

23 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; }

24 ... RGBColor penColor = new RGBColor(); pen.getRGBColor(penColor); System.out.println("red = " + penColor.red + ", green = " + penColor.green + ", blue = " + penColor.blue);...

25 Access

26 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

27 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());...

28 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

29 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; ^

30 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


Download ppt "Classes and Objects Introduction Life Cycle Creation Example."

Similar presentations


Ads by Google