LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   What does it mean when I read "Something has a reference to something else"? (https://www.linuxquestions.org/questions/programming-9/what-does-it-mean-when-i-read-something-has-a-reference-to-something-else-915612/)

puppymagic 11-26-2011 05:07 AM

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!!

fatmac 11-26-2011 05:32 AM

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.
#######

Proud 11-26-2011 05:42 AM

No, new and constructors (Classname()) are when you create new instances in OOP.
Quote:

Originally Posted by fatmac (Post 4534289)
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.

fatmac 11-26-2011 06:59 AM

@Proud
Thanks for correcting,
didn't do very much c++, didn't really need it, went back to c.

MTK358 11-26-2011 08:21 AM

Quote:

Originally Posted by puppymagic (Post 4534275)
0 down vote favorite
share [fb] share [tw]

???

Quote:

Originally Posted by puppymagic (Post 4534275)
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?

What is "the activity"? What variable contains it in the snippet you provided?

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)
{
    value += 3;
}

void add3UsingPointers(int *value)
{
    *value += 3;
}

int main()
{
    int i = 0;
    add3UsingPointers(&i);
    add3UsingReferences(i);
}


Proud 11-26-2011 08:30 AM

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.

Sergei Steshenko 11-26-2011 12:33 PM

Quote:

Originally Posted by Proud (Post 4534295)
No, new and constructors (Classname()) are when you create new instances in OOP.
...

No all OO languages create objects through 'new'. E.g. in Perl 'new' can have any other name - it's a user defined name.
...
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.