java - What does the "this" keyword as a parameter do exactly? -


this question has answer here:

so have searched lot -and "a lot" mean it-, both in website , in other ones, realize keyword this in java.

i following tutorials these days develop game android. in these tutorials, uploader puts "this" parameter doesn't explain why it.

what know far:

  • it can used parameter (this part confused)
  • it can put fish.this refer outer class (not sure one)
  • it can used refer refer outer variables (worst definition ever, know) this:

public class humans{

    int name; //aka name1     public humans(int name){ //aka name2         this.name = name; //the name1 = name2     } 

i'd have in depth explanation of keyword since find confusing and, @ same time, prevents me moving on tutorials (please don't bother answering if response going brief, have things clear in mind because confused easily, in programming). stuck , appreciated!

within instance method or constructor, reference current object — object method or constructor being called. can refer member of current object within instance method or constructor using this.

the common reason using keyword because field shadowed method or constructor parameter.

for example, point class written this

public class point {     public int x = 0;     public int y = 0;      //constructor     public point(int a, int b) {         x = a;         y = b;     } } 

but have been written this:

public class point {     public int x = 0;     public int y = 0;      //constructor     public point(int x, int y) {         this.x = x;         this.y = y;     } } 

each argument constructor shadows 1 of object's fields — inside constructor x local copy of constructor's first argument. refer point field x, constructor must use this.x.

you can find example here(also show diffrent uses of "this" keyword) ---> http://docs.oracle.com/javase/tutorial/java/javaoo/thiskey.html


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -