Home | Back | Contents | Next |
Note: In standard Java, a method inside of an object (an instance method) may refer to the enclosing object using the special variable 'this'. For example: // MyClass.java MyClass { Object getObject() { return this; // return a reference to our object } }In the example above, the getObject() method of MyClass returns a reference to its own object instance (an instance of the MyClass object) using 'this'. |
// Define the foo() method: foo() { int bar = 42; print( bar ); } // Invoke the foo() method: foo(); // prints 42 print( bar ); // Error, bar is undefined here |
foo() { int bar = 42; return this; } fooObject = foo(); print( fooObject.bar ); // prints 42! |
foo() { bar() { ... } } |
foo() { int a = 42; bar() { print("The bar is open!"); } bar(); return this; } // Construct the foo object fooObject = foo(); // prints "the bar is open!" // Print a variable of the foo object print ( fooObject.a ); // 42 // Invoke a method on the foo object fooObject.bar(); // prints "the bar is open!" |
foo() { bar() { } if ( true ) { bar2() { } } return this; } |
Home | Back | Contents | Next |