Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on a mock question..

 
andy armstrong
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will be the output on execution of the following code in a Java Server Page (JSP)?
<html>
<body>
<% int count; %>
<% for (int i=0; i<10; i++) { out.println("The counter is :"+count); } %>
</body>
</html>
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tomcat said count may not have been initialized because it got parsed into in _jspService():

does the book give a different answer?
[ May 12, 2003: Message edited by: Erick Reid ]
 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that code will make Runtime Exception because variable count have not been initialized...
Correct me if i am wrong
daniel
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, I agree you'll get a compiler error complaining that "count" was not initialized.
But what happens if you add one tiny little character.... like this:
<html>
<body>
<%! int count; %>
<% for (int i=0; i<10; i++) { out.println("The counter is :"+count); } %>
</body>
</html>
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of being a local variable it becomes a static varable, which is initialized to 0 and will retain its value until the jvm is stopped or restarted.
[ May 12, 2003: Message edited by: John Hembree ]
 
Arvind Chavar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I may correct it becomes an instance variable and not a static variable.
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arvind Chavar:
If I may correct it becomes an instance variable and not a static variable.

I suppose I could be confused about this then, from SCWCD Exam Study Kit:
"Declarations declare and define variables and methods that can be used in the JSP page, Theoretically, a JSP declaration can contain any valid Java declaration including inner classes and static code blocks."

"This declares a variable named count and initializes it to 0. The variable is initalized only once when the page is first loaded by the JSP engine, and retains its value in subsequent client requests. That is why the count variable is not reset to 0 each time we access the page."
page 178
initalized only once sounds like a static variable to me.
Could someone clarify this???
 
Amer Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<html>
<body>
<%! boolean b; %>
<%! int count; %>
<% for (int i=0; i<10; i++) { out.println("The counter is :"+count); } %>
</body>
</html>
Here 'count' is an instance variable,which being an int will initialize to 0(default).If it were a boolean then it would initialize to false(default)
(b will initialize to 'false').
"Dont worry,be Happy"
Amer
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
initalized only once sounds like a static variable to me.

Sorry all I was having a java brain fart. I got to thinking about it some more and realized that it would need to say <%! static int count; %> before it would be a static variable.
When I first answered the question, I typed in that it was an instance variable and then went back and confused myself and changed it. Got to quit doing that.
[ May 13, 2003: Message edited by: John Hembree ]
 
Arvind Chavar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As there is only one servlet(default behaviour) servicing all the worker threads.So any instance variables are shred by all the threads.Hence you get a feeling that it is static variable, but is not so.
In the default threading model for servlet the instance variable and the static variable have the same effect, but you could observe the difference if your servlets use single thread model.In single thread model there could be more than one instance of the servlet servicing the user requests.In such case static variable will be shared by all instances of servlet, hence could be accessed in any thread being serviced by servlet of this type.Whereas instance variables are accessible by only the threads accessing a particluar instance.

Hope this clarifies your understanding of static and instance variables against the servlet threading model.
Arvind
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However since this is the certification forum for SCWCD, I need to know for sure on the exam.
<%! is a declaration, which means it will not be part of _jspService and therefore will not be a local variable. In this context, it would be an instance variable regardless of the threading model. It would have to be declared <%! static ... before it would be a static variable regardless of the number of threads.
I also understand the concept of the SingleThreadModel which would keep multiple instances from being created and thereby have the same effect as instance/static.
 
Arvind Chavar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%! is a declaration, which means it will not be part of _jspService and therefore will not be a local variable.
Ans : TRUE.It will be an instance variable.
In this context, it would be an instance variable regardless of the threading model. It would have to be declared <%! static ... before it would be a static variable regardless of the number of threads.
Ans : TRUE
I also understand the concept of the SingleThreadModel which would keep multiple instances from being created and thereby have the same effect as instance/static.
Ans : Not true.SingleThreadModel only ensures that there will be only thread using this instance.This effect could be achieved by the servlet container in either of the two following ways.
First : Create only one instance of servlet.Queue up the requests and handle one request after the other in sequence.In this case instance and static variable have equivalent effect.
Second : Create many instances of servlet.In such a case there will be as many instance variables as there are instances of servlet, where as there is one and only one static variable.This static variable is avaialable to all instances of servlets.
 
andy armstrong
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code explains things very clearly.
the scriplet code is reinitialized each request
whereas the declartive code is incremented
and retains the value.
<%! int i; %> //Instance variable
<% int j = 0; %> //Local variable
<% out.println("i is :" + i++); %>
<% out.println("j is: " + j++); %>
Thanks for the great discussion guys.
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic