• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Declarations  RSS feed

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I have some doubt abt the declarations. I know that if we declare a variable like <% int x = 3 %>, then it will be in the service method, and local to it, and if we declare a variable as <%! int x = 3 %> then it will be in the class level, here the scope is upto the class, is it possible to access the same variable through out the application? what does class scope means here.

Thanks in advance,
Srini.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what does class scope means here.



You can access that variable in jspInit and jspDestroy also. While former was accessible inside service method only.



Is it possible to access the same variable through out the application



Technically Yes! Since there is only one instance of a servlet so this variable will be one for whole application. But it would require a lot of extra stuff and also using context parameter is far better option.

please correct me if I am wrong?
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are in SCWCD forum.. I assume that you are SCJP

And a class level variable (non-static/instance variable) can be accessed by all the members of the class (So, in servlet, it can be accessed in init(), service(), doxxx(), destroy() and other user defined methods)
 
sri rallapalli
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi arvind,
U said, technically it is accessible throughout the appllication. so now my question is, if we declare another variable with the same name in another JSP, then how can we access the variables differently.
Sri.
 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

how can we access the variables differently



probaby you will need to do -

some_getter_method_that_returns_instnace_of_the_current_request
_thread's_servlet( ) . variablename !!

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!