Any example like without using 'this' pointer we cant have a solution?
Printable View
Any example like without using 'this' pointer we cant have a solution?
bar = bar would have no effectCode:class Foo{
private int bar;
public Foo(int bar){
this.bar = bar;
}
}
/edit: Advanced java? :x:
but i can write like
class Foo{
public Foo(int a){
bar = a;
}
}
without using this
This refers to the current object. Look into shadow variables, as well as local and instance variables.
@zyril ,can you please tell me, can I have a problem like without This i cant have a solution?
I think so. Which raises the question... why?
Without much thought I'll hazard the guess that you never actually need "this" unless nested classes are involved when it's necessary to disambiguate types rather than methods or variables. (someone will doubtless correct me if I'm wrong).
But the real question is why you would prefer some contrived, verbose, frown inducing, eyebrow raising alternative when "this" would do the job nicely.
If #8 is not clear, ask.
My point was that if using "this" is clear and straight forward, use it.
(If you google "java nested class this" you will find pages discussing the cases where "this" has to be used. Again, ask if they are unclear.)