Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

In java there is no problem with forward referencing ?

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They must be talking about something else. Which chapter are you reading ?
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 2418
95
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic