What does it mean when I read "Something has a reference to something else"?
0 down vote favorite
share [fb] share [tw] onCreate(Bundle State) { TextView label = new TextView(this); lable.setText("My new Label); } I read that the TextView has a reference to the activity. what does it mean for something to have a reference on something else? Thanks a lot, experts @Linux Questions!! |
I think it is when you create a new instance in object oriented programming,
long time since I did any. new TextView(this) is referencing object TextView to create a new instance. ###### TextView label = new TextView(this); lable.setText("My new Label); I read that the TextView has a reference to the activity. ####### |
No, new and constructors (Classname()) are when you create new instances in OOP.
Quote:
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. |
@Proud
Thanks for correcting, didn't do very much c++, didn't really need it, went back to c. |
Quote:
Quote:
Anyway, assuming this is C++, a reference is like a pointer, but the compiler automatically dereferences it for you whenever you use it. The only reason to use them over pointers is that it looks nicer. Inside, they work the same way. For example, here is an eaxmple of a function that increments a variable passed to it. One uses pointers, and another uses references: Code:
void add3UsingReferences(int &value) |
Being as the thread's been tagged Android and Java since I before I first posted I assumed it was Java, and that the first few lines are an over-zealous copy-paste from some site with social network integration.
OP asked the same question all over the net. http://stackoverflow.com/questions/8...ment-questions http://ca.answers.yahoo.com/question...6030915AACWPY7 The original bad snippet seems derived from something touched on here http://developer.android.com/resourc...ory-leaks.html or just http://developer.android.com/resourc...llo-world.html But I would strongly advise the OP to understand any simple programming before trying to handle the quirks of the relatively-constrained environment of Android devices. |
Quote:
... Regarding the OP question - the answer is more general. One can access something as a whole directly, like one can be handed a loaf of bread, and one can be said: "you'll find the loaf of bread on the kitchen table". In the latter case the kitchen table is a reference to the loaf of bread. Or one can say: "you can find the loaf of bread on the table which is located in the kitchen". In this case the kitchen is reference to the table, and the table is reference to the loaf of bread. In the OP's case the 'label' variable is also often called a handle (to a GUI element). A similar analogy - a (heavy and full o treasures) suitcase is dealt with through its handle. |
All times are GMT -5. The time now is 04:28 PM. |