Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help with variable modifiers  RSS feed

 
Michael Hul
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is my problem basically:
I have first a jsp with a form that when submitted runs some java code and in particular sets a stream variable(class variable) with pdf content. Now after the variable is set and the process finishes, I need to have access to the value of that variable when another form is submitted before that value is set again to another stream. Unfortunately i get that value as null on the second run to the server if the variable is not declared static, and if it is declared static, then I can't change the value later to the next pdf stream.
Basically what I'm asking is 1) Is there a way to change the value of a static class variable? OR 2) Is there a way to preserve a non-static variable throughout the life of the JVM so that the value is not automatically reset to null?

Thanks in advance
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Hul:
1) Is there a way to change the value of a static class variable?

As long as it isn't final, you can always reassign it. The only difference is you don't need an instance to access it.

2) Is there a way to preserve a non-static variable throughout the life of the JVM so that the value is not automatically reset to null?

Since this is JSP you're talking about, I would say put the variable in the session (preferred if it should only be stored per visitor), or perhaps even for the entire application. Check out the different possible scopes for JSP / servlet variables. JSP has 4: page, request, session and application.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Rob already pointed above, most probably you need a session scoped variable or a context/application scoped variable

Is there a way to preserve a non-static variable throughout the life
of the JVM so that the value is not automatically reset to null?


In a web environment your emphasis should be on the life of the application since more than one JVM's may be involved.
 
Michael Hul
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as it isn't final, you can always reassign it. The only difference is you don't need an instance to access it.

How exactly would I go about reassigning it. Currently I have the variable stored in a class called 'pdfStore' so I can set it from another class using Then inside of a servlet I access it using This works for the first time that the variable pdfStream is set but when I run that code again and set the stream to something else (or null for that matter) when i access it, it only gives me the old value of the variable, as if it wasn't set again.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Listen to the voice of reason and ditch the static variable. That way lies heartache and pain. Store information that needs to span requests in the session.
 
Michael Hul
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replys, once again my short-sightedness has been my downfall. I should really learn to better review my code before posting questions. The session variable is the way to go.
Thanks again.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!