Results 1 to 2 of 2
- 05-14-2012, 11:57 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 48
- Rep Power
- 0
- 05-15-2012, 12:52 AM #2
Re: What does it mean to reference the current object?
So when we write a java class, and instantiate that class as an object, the code executing inside said object and reference itself using the keyword this. It is a common idiom when working with constructors or setter methods, but can be used all over the place.
For example:
In this example, we have a constructor that takes a parameter called 'a' which coincidentally is also the name of the declared instance variable above it. When we say "this.a = a;" we're literally saying "the 'a' belonging to the Alpha instance we are currently in should be assigned the value contained in the parameter named 'a'".Java Code:public class Alpha{ int a; public Alpha(int a){ this.a = a; } }
You'll also see it used quite a bit when working with actionListeners and GUIs - frequently one will implement an interface and then pass a reference to the current class (this) to a method that assigns an action listener.
Similar Threads
-
Reference of an object
By saiganesh44 in forum New To JavaReplies: 7Last Post: 04-21-2012, 11:22 AM -
What to do with object reference?
By kyle_maddisson in forum New To JavaReplies: 6Last Post: 11-04-2011, 05:58 AM -
object and reference
By aizen92 in forum New To JavaReplies: 11Last Post: 04-01-2011, 08:39 PM -
Object and reference
By katie in forum New To JavaReplies: 2Last Post: 10-19-2009, 03:45 PM -
Getting the Object Reference Name
By Deathmonger in forum New To JavaReplies: 2Last Post: 03-12-2008, 02:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks