LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-26-2011, 05:07 AM   #1
puppymagic
Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 48

Rep: Reputation: 4
Red face 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!!
 
Old 11-26-2011, 05:32 AM   #2
fatmac
Member
 
Registered: Sep 2011
Location: Upper Hale, Surrey-Hants Border
Distribution: Debian & Debian based distros
Posts: 228

Rep: Reputation: Disabled
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.
#######
 
Old 11-26-2011, 05:42 AM   #3
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
No, new and constructors (Classname()) are when you create new instances in OOP.
Quote:
Originally Posted by fatmac View Post
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.

Last edited by Proud; 11-26-2011 at 05:53 AM.
 
1 members found this post helpful.
Old 11-26-2011, 06:59 AM   #4
fatmac
Member
 
Registered: Sep 2011
Location: Upper Hale, Surrey-Hants Border
Distribution: Debian & Debian based distros
Posts: 228

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

Last edited by fatmac; 11-26-2011 at 07:00 AM.
 
Old 11-26-2011, 08:21 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by puppymagic View Post
0 down vote favorite
share [fb] share [tw]
???

Quote:
Originally Posted by puppymagic View Post
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);
}
 
Old 11-26-2011, 08:30 AM   #6
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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.

Last edited by Proud; 11-26-2011 at 08:35 AM.
 
1 members found this post helpful.
Old 11-26-2011, 12:33 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by Proud View Post
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.
 
  


Reply

Tags
android, java


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] du says "cannot read directory" and "permission denied", but I have rwx permissions mrtwice Linux - Software 14 04-21-2010 12:38 PM
Getting "Short read in buffer_copy" error with "dpkg" stalefries Linux - Software 6 03-24-2010 03:19 PM
"Multiple definition of" and "undefined reference to" in QT Creator with QJson lib echelon89 Programming 1 05-01-2009 06:48 AM
Edit "read-only" /etc/fstab from "System Recovery#" (without boot cd?) charlweed Linux - General 1 12-31-2006 05:57 PM
Undefined Screen " XFree86" reference by serverLayout "XFree86 Configured" comox *BSD 7 01-17-2005 05:47 PM


All times are GMT -5. The time now is 03:06 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration