• Post Reply Bookmark Topic Watch Topic
  • New Topic

does variable declared in declaration automatically become static  RSS feed

 
nirali shah
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going through head first servlets & jsp book on page no 295.

I came across this example, in which count is initialized in expression now count is incremented by one. Next time it comes in the loop, it again is assigned with zero. Since the variable declared in expression becomes local variable.

Now the same variable count is declared in declaration. So it becomes class variable. Now this variable count is accessed in the local method. Now the question is how can a variable be initialized at class level , unless its a static variable. The variable show in example is not declared as static (As its in a servlet which is derived from jsp page). So does a variable declared in declaration automatically become static ?

thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. It becomes an instance variable of the generated servlet. But since many threads my share this same servlet, this is a dangerous thing to do.

All scriptlets, including declarations, should no longer be used in JSPs. It's much safer to use modenr JSTL and EL mechanisms.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!