Forward Declaration and "this" keyword
Consider this code:
class A{
privateint i = 2 * this.j;// This will be calculated as 2 * 0 = 0
//private int i = 2 * j; //This code will not compile
privateint j = 20;
}
Why the "this" keyword is required for the j to be accessible? Though it is a forward declaration, what is the significance of "this" which gives visibility to the variable j. Please give some light to this.
> Though it is a forward declaration, what is the significance of "this" which gives
> visibility to the variable j
I don't think "this" alters the visibility of j: that is the instance variable j is in scope. However "Use of instance variables whose declarations appear textually after the use is sometimes restricted, even though these instance variables are in scope."
See "8.3.2.3 Restrictions on the use of Fields during Initialization" http://java.sun.com/docs/books/jls/third_edition/html/classes.html#287410
Using "this" you have a reference to the object being constructed with the j instance variable sill having its default value of zero.
Such instance initialisers would appear to be inherently less intelligible than using a constructor.