Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF Static Bean Variable Scope

So, I've seen this post: JSF - session scoped bean shared by browsers on different machines

But this was a question from two years ago, so I don't know if there have been any updates in the world of JSF since then, and I also have some more specific cases to which I'd seek clarification. Basically, what I'd like to know is how static scope variables are handled in beans with different scopes. For example:

@ManagedBean
@ApplicationScoped
public class ApplicationBean{
    static private int someStaticInt=0;

    ...
}

Since this bean is Application scoped, I would completely expect someStaticInt to be shared by all users of the app, i.e. user A sets the value to 3, all users would henceforth see that value as 3. Correct me if I'm wrong.

But what about this scenario:

@ManagedBean
@ViewScoped
public class ViewScopeBean{
     static private int staticInt = 0;
     private SomePOJO myClass;
     ...

     public void someAction(){
         SomePOJO.memberStaticInt++;
         ...
     }
}

...

public SomePOJO{
    static private int memberStaticInt = 0;
    ...
}

Now, this bean is ViewScoped, so there's a separate instance for each user of the application. But what about that static int? If I increment that, is it only going to be within MY instance of the Bean, or is it going to be incremented for all users. Furthermore, what about that member object, myClass? It's not declared static in the bean, but it has a static member itself. If I run someAction, will memberStaticInt be incremented for all users or just the user using that instance of the Bean?

Finally, I'd be interested to know if any and all such logic in the above cases also applies to RequestScoped beans.

like image 917
user470714 Avatar asked Feb 23 '23 09:02

user470714


1 Answers

JSF Scopes won't change the meaning of static. Static still means static so regardless of your JSF scope that value will be shared by all instances of that class within the same VM.

like image 164
digitaljoel Avatar answered Mar 03 '23 18:03

digitaljoel