aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "error?" Watch "error?" New topic
Author

error?

Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531


above code compiles wheres this doesnt

Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75


In this code during the execution of the program first the compiler declares and intializes the variable and then after that the instance block executes.




In the above code the variable i is not even declared whether it is an integer ,float or what it is whereas on the first code we are declaring it.Hence it will give your a compile time error of identifier expected.
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
then why is there an error in this?
class Forward
{
{
System.out.println(i);
i = 10;

}

int i = 5;


}

isnt i already declared when it reaches system.out?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Ankur, please UseAMeaningfulSubjectLine for your topics.

Do you think this will compile

You can't have anything other than method declarations (including constructors), variable/constant declaration and static and instance initializer blocks in your class body...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75


This System.out gives you an error because it is not the correct forward referencing to correct this you have to use this.i so it specifically points to the reference variable and gives the output.

Well, in the case of forward referencing, we're not trying to read from i, we're assigning a value to it. Therefore, there's no "dangerous" operation occurring here and the compiler allows it. For more information check this link of java ranch I hope it will make it more clear...



Webpage for Forward Referencing
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Use java decompiler to get the idea how forward ref is resolved by compile, what compiler is doing internally. How code are placed in the last and executed.


SCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: error?