Objects and Reference Variables Example with Java Juhani
Objects and Reference Variables Example with Java © Juhani Välimäki 2004
Objects Reference variables Object is an instance of some certain class. It has the properties and functionality defined in that class. Object still has it’s own internal state, i. e. individual values saved in it’s attributes private variables. Reference variable either points to one object or points nowhere, referred as null. The contents of a reference ( pointer) is actually the memory address the object (or null). Object has NO NAME! Object is created with the new operator and by calling the constructor of the class. Object will be created somewhere in the memory. So you have to keep track of the pointers. Concepts a object of the class Result in memory location 11 a 62 f 3 d Result() int tell() If no reference variable points to a certain object, the object is lost and the automatic garbage collection will delete the object. Result eka points 11 a 62 f 3 d 23 add(int) Result() int tell() add(int) Another object of the class Result in the memory location 11 ab 34 ee public class Result { int points; // gathered points public Result() { points = 0; } public int tell() { return points; } public void add( int added ) { points = points + added; } } Reference variables DO HAVE NAME. Reference variables are the only means we can access and use objects. points 0 Result same 11 a 62 f 3 d Result other 11 ab 34 ee Result empty null public class Test. Result { public static void main( String[] args ) { Result first = new Result(); Result same = first; Result other = new Result(); Result empty; } } Rule 1: New objects are created only with the new-operator. Rule 2: Refrence variable has the memory address of an object or null Rule 3: Only assignment (=) may alter value of any variable © Juhani Välimäki 2004
Objects in an array Reference Variables See in the picture how more than one reference variables in multiple arrays (result. Array, top. Three) or standing alone (most. Recent) may point to the same object. Referenc variable may be a single variable like most. Recent. In addition to that an item in an array might be a refrence variable. Object in an array 0 1 2 3 4 5 6 7 8 9 10 11 null Result[] result. Array = new Result[12]; for( int i=0; i<9; i++ ) { result. Array[i] = new Result(); } Result[] top. Three= new Result[3]; top_three[0]=result. Array[8]; top_three[1]=result. Array[0]; top_three[2]=result. Array[3]; Result most. Recent = result. Array[8]; 0 1 2 Objects in an array is actually not correct, although we say it like that. Correct: There are references to the objects in the array. © Juhani Välimäki 2004
-Exercisea) What does the program print? b) Draw a sketch where you can see the reference variables, where they are pointing, the objects and the contents of the objects at the end of the program. public class Testaa. Result { public static void main( String[] args ) { Result my = new Result(); System. out. println( my. tell() ); my. add( 200 ); Result friend; friend = new Result(); friend. add( 300 ); SKETCH EXAMPLE (methods can be left out) Result() points int tell() 17 Result leads. Now; if( my. tell() > friend. tell() ) { leads. Now = my; } else { leads. Now = friend; } leads. Now. add( 10 ); System. out. println( leads. Now. tell() ); to the Garbage Collector add(int) points 23 Result my Result other Result neighbour = new Result(); neighbour. add( 450 ); leads. Now = neighbour; leads. Now. add( 25 ); null public class Result { int points; // gathered points System. out. println( public Result() { points = 0; } public int tell() { return points; } public void add( int added ) { points = points + added; } ”---” ); neighbour. tell() ); friend. tell() ); my. tell() ); leads. Now. tell() ); friend = my; // Draw the sketch here! } } } Rule 1: New objects are created only with the new-operator. Rule 2: Refrence variable has the memory address of an object or null Rule 3: Only assignment (=) may alter value of any variable © Juhani Välimäki 2004
Solution step by step With the assignment (=) operation the variable my is set to point at the newly created object of Result class. The variable my now contains the memory address of the object. Result my = new Result(); Result() points Result my int tell() 0 add(int) New reference variable my is defined. It may point to one object of the Result class or nowhere (null). public class Result { int points; // gathered points New object of the Result class is created with the new operator and by calling the constructor of the Result class public Result() { points = 0; } public int tell() { return points; } public void add( int added ) { points = points + added; } } Rule 1: New objects are created only with the new-operator. Rule 2: Refrence variable has the memory address of an object or null Rule 3: Only assignment (=) may alter value of any variable © Juhani Välimäki 2004
Method call System. out. println( my. tell() ); 0 Result() points The reference variable my was defined to be a reference to an object of the Result class: Result my int tell() 0 add(int) Result my; So after the method call my. tell() the class Result will tell what to do when tell() method is called. Result class tells to return the value of the varible points. public class Result { int points; // gathered points public Result() { points = 0; } Prints: 0 public int tell() { return points; } public void add( int added ) { points = points + added; } } © Juhani Välimäki 2004
Method call my. add( 200 ); Result() int tell() Result my points 200 add(int) public class Result { int points; // gathered points public Result() { points = 0; } public int tell() { return points; } public void add( int added ) { points = points + added; } } © Juhani Välimäki 2004
New reference variable Result friend; Result() int tell() Result my points 200 add(int) Result friend null public class Result { int points; // gathered points public Result() { points = 0; } public int tell() { return points; } public void add( int added ) { points = points + added; } } © Juhani Välimäki 2004
New object and new value for a reference variable friend = new Result(); Result() int tell() Result my 200 add(int) Result() Result friend points int tell() points 0 add(int) public class Result { int points; // gathered points public Result() { points = 0; } public int tell() { return points; } public void add( int added ) { points = points + added; } } © Juhani Välimäki 2004
Method call friend. add( 300 ); Result() int tell() Result my 200 add(int) Result() Result friend points int tell() points 300 add(int) public class Result { int points; // gathered points public Result() { points = 0; } public int tell() { return points; } public void add( int added ) { points = points + added; } } © Juhani Välimäki 2004
New reference variable Result leads. Now; Result() int tell() Result my 200 add(int) Result() Result friend points int tell() points 300 add(int) Result leads. Now null public class Result { int points; // gathered points public Result() { points = 0; } public int tell() { return points; } public void add( int added ) { points = points + added; } } © Juhani Välimäki 2004
New value for a reference variable 200 300 if( my. tell() > friend. tell() ) { leads. Now = my; } else { leads. Now = friend; } Result() int tell() Result my 200 add(int) Result() Result friend points int tell() points 300 add(int) Result leads. Now public class Result { int points; // gathered points public Result() { points = 0; } public int tell() { return points; } public void add( int added ) { points = points + added; } } © Juhani Välimäki 2004
Method call leads. Now. add( 10 ); Result() int tell() Result my 200 add(int) Result() Result friend points int tell() points 310 add(int) Result leads. Now public class Result { int points; // gathered points public Result() { points = 0; } public int tell() { return points; } public void add( int added ) { points = points + added; } } © Juhani Välimäki 2004
Method call System. out. println( leads. Now. tell() ); Result() int tell() Result my points 200 add(int) Result() int tell() Result friend points 310 add(int) Result leads. Now public class Result { int points; // gathered points public Result() { points = 0; } Prints: 310 public int tell() { return points; } public void add( int added ) { points = points + added; } } © Juhani Välimäki 2004
New object and reference variable Result() Result neighbour = new Result(); int tell() Result my 200 add(int) Result() Result friend points int tell() points 310 add(int) Result leads. Now public class Result { int points; // gathered points public Result() { points = 0; } public int tell() { return points; } Result() Result neighbour int tell() points 0 add(int) public void add( int added ) { points = points + added; } } © Juhani Välimäki 2004
Method call neighbour. add( 450 ); Result() int tell() Result my 200 add(int) Result() Result friend points int tell() points 310 add(int) Result leads. Now public class Result { int points; // gathered points public Result() { points = 0; } public int tell() { return points; } Result() Result neighbour int tell() points 450 add(int) public void add( int added ) { points = points + added; } } © Juhani Välimäki 2004
New value for a reference variable leads. Now = neighbour; Result() int tell() Result my 200 add(int) Result() Result friend points int tell() points 310 add(int) Result leads. Now public class Result { int points; // gathered points public Result() { points = 0; } public int tell() { return points; } Result() Result neighbour int tell() points 450 add(int) public void add( int added ) { points = points + added; } } © Juhani Välimäki 2004
Method call leads. Now. add( 25 ); Result() int tell() Result my 200 add(int) Result() Result friend points int tell() points 310 add(int) Result leads. Now public class Result { int points; // gathered points public Result() { points = 0; } public int tell() { return points; } Result() Result neighbour int tell() points 475 add(int) public void add( int added ) { points = points + added; } } © Juhani Välimäki 2004
Method calls System. out. println( ”---” ); neighbour. tell() ); friend. tell() ); my. tell() ); leads. Now. tell() ); Prints: --475 310 200 475 (points to the same object as neighbour) Result() int tell() Result my 200 add(int) Result() Result friend points int tell() points 310 add(int) Result leads. Now public class Result { int points; // gathered points public Result() { points = 0; } public int tell() { return points; } Result() Result neighbour int tell() points 475 add(int) public void add( int added ) { points = points + added; } } © Juhani Välimäki 2004
New value for a reference variable and one object will be lost friend = my; Result() int tell() Result my points 200 add(int) to Garbage Collector Result() Result friend int tell() points 310 add(int) Result leads. Now public class Result { int points; // gathered points public Result() { points = 0; } public int tell() { return points; } Result() Result neighbour int tell() points 475 add(int) public void add( int added ) { points = points + added; } } © Juhani Välimäki 2004
- Slides: 20