*
The moose likes Beginning Java and the fly likes Why it gives ERROR without 'this' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why it gives ERROR without Watch "Why it gives ERROR without New topic
Author

Why it gives ERROR without 'this'

faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
hi, can anybody explain me the reason ,Why is to so with(this.i) it does not gives compile time error.

public class Sequence
{
public Sequence() {
System.out.println(" constructor initializer : "+ i );
}

{// line x
System.out.println(" instance initializer :"+this.i);
}

static
{
System.out.println("static initializer");
}

public static void main(String args[])
{
Sequence sq = new Sequence();
}
int i = 20;
}
output is :
static initializer
instance initializer :0
constructor initializer : 20

If at // line x i edit it like this
System.out.println("instance initializer :"+ i );
it gives compile time exception :
illegal forward reference
System.out.println("instance initializer :"+ i );
^
Rodrigo Alvarez
Ranch Hand

Joined: Apr 10, 2006
Posts: 75
This compiles and run :



But the output is now:



It is a mistake to think you can solve any major problems just with potatoes.<br />--Douglas Adams
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
I think you are using java 1.5 , actually on my comipiler it is showing me a compiler error .

illegal forward reference
System.out.println("instance initializer :"+ i );
^

Hope any body else throw some light on this too.
[ April 15, 2006: Message edited by: faisal usmani ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18486
    
  40

Initialization of instance variables, and executions of instance initializers, are done in order. In this case, it matters on how you typed it in -- think of it as running linearly from top to bottom.

This means that you should not be using forward references -- as variables later in the class are not initialized during initialization of eariler variables (or instance initializers). Actually, they are initialized (as you have seen), but with temporary default values -- like 0, 0.0, or null.

The compiler will do some checking to make sure that these forward references does not exist... but it does a poor job. One way to "trick" it is to reference it, by dereferencing an object -- as you have done by using the this variable. Another way is to call a method which uses it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
Thanks Henry

You made it all clear

--There is no duty more obligatory than the repayment of kindness.
[ April 15, 2006: Message edited by: faisal usmani ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why it gives ERROR without 'this'
 
Similar Threads
Instance Initializer
scope
JLS 14.17 throw unchecked exception in instance block
Order of execution and variable access
going bananas with this one