Question about Java tutorial 44(3:28)

0 Kirill Lukenyuk · May 6, 2015
Java for beginners tutorial 44(3:28)
Maybe I missed it(most possibly) or it wasn't mentioned but why the name of those variables suppose to be different??

Thanks everybody for answering :)

Post a Reply

Replies

Oldest  Newest  Rating
0 Kuroodo Ditory · May 8, 2015
Not sure why. I guess to keep things basic and simple since it's the beginners tutorial.

I dunno if it doesn't work with contant variables like in the vid, but you should be able to do





private final String desc; // Global variable

tuna(String desc){

this.desc = desc;

}


this.desc refers to the desc variable we created outside of the constructor (the global one).

In English this says: Set the value of the global variable desc, equal to the value of the desc variable in the parameters (or you can call it the local variable).
0 Nicholas Eason · May 8, 2015
I believe that he said the names need to be different to avoid using the `this` keyword (i.e Kuroodo's response). They don't actually need to be different. Later on, you'll learn about overriding (dont know if Bucky goes over this concept).

Overriding is like making a variable twice:


private int x;
public static void main(String args[]){
int x;
}

 
But because of something called Scope (also dont think bucky goes over this) the program will prioritize the locally-declared variable. To get any reference to the private int x I created above, I need to reference it using

this.x


this.x is saying use the X inside of THIS. 'this' being the class in this case.
0 Kirill Lukenyuk · May 8, 2015
thank you!
  • 1

Java / Android Development

107,182 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator