• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Declarations

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic