A little explaination on various parts of code

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

A little explaination on various parts of code

Postby digitality » Tue Jan 27, 2009 5:26 am

Hello,

I am new to this forum and I hope I haven't stepped on any toes by posting in this specific section, I am not quite sure which one to ask a question in or if it would be considered rude to do so at all in these forums but here goes.

I was wondering if anyone would explain to me a little about this snippet of code for a simple hello world:

@Override
// What is @Override?


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


TextView tv = new TextView(this);


// Okay, so I understand that tv is of type "TextView" but why the = new TextView(this);?
// I don't quite understand what "new" is for and why there is an argument called "this"


The rest of it seemed to make plenty of sense.

I hope you can help me out a little.. I am definitely a newbie but I am eager to understand everything in extreme detail which I can't seem to find so easily in most tutorials I viewed (although I haven't checked yours yet, I am surfing looking for a place to learn more and a good forum to bug people for help from until I can actually contribute back)

Best Regards,
-Digitality
"In the confrontation between the stream and the rock, the stream always wins; not through strength, but through persistence." - Buddha
digitality
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jan 27, 2009 5:11 am

Top

Re: A little explaination on various parts of code

Postby MrSnowflake » Tue Jan 27, 2009 11:54 am

digitality wrote:I am new to this forum and I hope I haven't stepped on any toes by posting in this specific section, I am not quite sure which one to ask a question in or if it would be considered rude to do so at all in these forums but here goes.
Don't worry, you posted in probably the best subforum for this kind of topic.

digitality wrote:I was wondering if anyone would explain to me a little about this snippet of code for a simple hello world:
@Override
// What is @Override?


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


TextView tv = new TextView(this);


// Okay, so I understand that tv is of type "TextView" but why the = new TextView(this);?
// I don't quite understand what "new" is for and why there is an argument called "this"


The rest of it seemed to make plenty of sense.

I hope you can help me out a little.. I am definitely a newbie but I am eager to understand everything in extreme detail which I can't seem to find so easily in most tutorials I viewed (although I haven't checked yours yet, I am surfing looking for a place to learn more and a good forum to bug people for help from until I can actually contribute back)
Yet, as you seem to not know Java very good, I suggest going over the Sun Java tutorials, so you know what Java is and what it can do for you. You are never going to write good applications, without knowing Java properly. If you read the basic tutorials and still have question you are of course allowed and very welcome to pose them here.

I say this, because you have no idea what such a fundamental keyword as 'new' means and does. If you don't know stuff like this, you should go and learn Java itself first, so you are able to make some learning projects. If you are there, you can come back to us for more info, but we are not here to learn Java itself to you, that's what Sun made those great tutorials for. Yet, I don't want you to go away, I just want to help you in getting to know Java properly.

I've made a post not so long ago to help out Android and Java newbies. Please check it out and come back if you have more questions.

Now to help you with these questions @Override is an annotation which explains to the compiler that you are aware that you are going to override the previous functionality of the method with the same name, which exists in one of the classes you inherit from.
'new' allows you to make a new object of the type you specify, and it allocates space in memory to do so.
'this' is used as a parent to the TextView object, but this is Android specific and the SDK will tell you when you have to specify an Acitivity or Context to an object (or View). 'this' in this case is either of the type Context, as that's what the constructor wants for a paramter, but to make it more comples, 'this' actually is of the type Activity. It's polymorph which means it can have the type of every of it's inherited classes or interfaces.

Snow
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to General

Who is online

Users browsing this forum: lethanh and 5 guests