This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes In java there is no problem with forward referencing ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "In java there is no problem with forward referencing ?" Watch "In java there is no problem with forward referencing ?" New topic
Author

In java there is no problem with forward referencing ?

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
On page 293 Head First Servlets and Jsp its written there is no problem with forward referencing.

Then why doesnt the below compile :

void foo()
{

int y = 5+x;
int x=2;
}

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

They must be talking about something else. Which chapter are you reading ?


[My Blog]
All roads lead to JavaRanch
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Are you sure you are talking about Head First Servlets and JSP book and not Head First Java?? There is nothing like that on page 293 of my HFSJ book...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
Hi Ankit,

The below is there on page 293:

JSP:


The JSP becomes this servlet:

public class basicCounter_jsp extends HtpServlet
{
int doubleCount()
{
count = count*2;
return count;
}
int count=1; // It's java so no problem with forward-referencing(Declaring the variable AFTER you used it in a method)
}


My question here is Then why doesnt the below compile :

void foo()
{

int y = 5+x;
int x=2;
}
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Local variables do not behave the same way as instance variables. The example in your book is about an instance variable, which is why it compiles. This is further explained here.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1644
    
  23

Christophe is correct and to elaborate a bit more on this you have to know that the page is first translated into a Java file before it is compiled. During the translation phase the declarations are moved outside the _jspService() method. So in this case both the method as the declaration of count do not end up in the _jspService() method. count becomes an instance variable and doubleCount() an instance method.

If you make them local to the _jspService() method, let's say like this (you can't have a method inside a method)

then the compiler will definitely complain that count isn't defined.
Regards,
Frits
Vinayak Bhat
Ranch Hand

Joined: May 15, 2008
Posts: 55
So when the jsp is compiled, count becomes the instance variable. and doubleCount() becomes the operation as in a method or a function. So that's completely acceptable.



- Vinayak Bhat
SCJP 5 OCEJPAD6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: In java there is no problem with forward referencing ?
 
Similar Threads
forward referencing
static variables
Forward variable referencing.
Intializers
inner class using forward reference doubt