and constructors (Classname()
) are when you create new instances in OOP.
Originally Posted by fatmac
new TextView(this) is referencing object TextView to create a new instance.
TextView is the name of the class which this line will cause a new instance to be created for. The reference in this snippet is the this
keyword, which means we'll be using the constructor method with a signature that takes a single argument/parameter, of the type of whatever class is calling this constructor right now (if that actually matches with one of the TextView constructor definitions and actually compiles).
Of the OP's quote, label
is also a reference.
References are variables that store only a reference to an Object type (instead of primative variables that store scalars such as of type int
, etc), they are only as large as any other reference behind the scenes, they don't cause copies of the referenced object to be created. You almost always pass (copies of, and this is where it can get a bit confusing)references to methods and data structures, because making copies of every object would be inefficient and complicate/make impossible things such as searching and sorting. I suggest you google/wikipedia these basic OOP concepts.
OP, would you care to link where you're quoting from, and use [code] tags for future snippets?
I believe in the context of your example snippet, whereever this onCreate() method is being defined would be within the 'activity' class you mention, such that the 'this' self-reference would then be to the activity instance. However the snippet seems useless on first glance, it's creating a TextView, setting its text, and then label
isn't obviously added to any other data structure, and so is liable to being unreferenced and garbage-collected, rather than persisting. In short, it might only exist while this method is executing, and no one or thing will see it.
Also the String "My new Label
isn't closed and thus won't compile.