0

I have a bean class as follows:

static int variable_static;

public RestaurantBean() {  
    variable_static = 0;  
}

//...  

//value of variable_static  changes throughout the code  
//...

public string button() {  
    //firing this button causes the variable_static value to change back to 0
    return null;
}

I have a static variable which its value undergoes changes throughout the code (I know it by printing out its value), but when the button is fired, its value automatically changes back to the default value which is what not I want. Is this a postback in JSF? how can I prevent this from happening?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Z.V
  • 1,441
  • 3
  • 21
  • 35
  • Are you using JSF 1.x or JSF 2.x? – Luiggi Mendoza Aug 03 '13 at 20:34
  • Looks like you're using a `@RequestScoped` bean, change it to `@ViewScoped` at least and try again. – Luiggi Mendoza Aug 03 '13 at 20:36
  • 3
    This is a really strange piece of code. A static variable is set in a constructor? Wouldn't you rather set it during initialization or in a static block? By the way, if you switch to `@ViewScoped` and it "works" for you, then you should remove `static` as well, otherwise you're going to share the data among all visitors. All with all, this is pretty basic Java. I recommend to take a JSF pause and take a step back learning basic Java. Learn what `static` means. Learn what a constructor is. – BalusC Aug 03 '13 at 20:48

2 Answers2

2

The problem is that since it is a @RequestScoped bean, it is being created on every request (including ajax requests), and the constructor reinitializes the static variable to 0 (the default value). This is done in your constructor, by the way:

public RestaurantBean() {
    //this is the culprit
    variable_static = 0;
}

Knowing this, the solution would be changing the scope of the bean to a wider one, like @ViewScoped or @SessionScoped (depending on your needs).

After this, now what BalusC and me think: why to use a static field? At least that the only purpose of this field is to survive along multiple requests in the same view (that is already accomplished by @ViewScoped), then there's no reason to mark it with the static modifier unless you share this variable with other classes (with would be an odd design of your application).

More info about this:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

Assuming you have your @ManagedBean set to @RequestScope the reason the value resets itself is that the bean gets created each time a new request is received. This happens when you click the button. The constructor is invoked each time an instance of that class is created and therefor the value of your variable is reset.

As was suggested above you "could" try a @ViewScope bean instead, but tbh, listen to what BalusC said in his comment. The real problem is that you use a static variable to track information. If you have a need to track information, you might consider using a session attribute instead.

Boris Remus
  • 191
  • 1
  • 4