4
votes

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.

1
If static behaved like as a non-static member, what would have been the point of static, do you think?BalusC
Basically, if I could refer to variables statically only within the scope of the bean, so for example, within a particular user's session that could sometimes be beneficial. Just for example, in the real world scenario my SomePOJO class is actually a Collection of objects of that class, but they have a member variable that can be shared across all objects only for that user -- it would be great if I could make it static. But not if that static member will be the same across all sessions!user470714

1 Answers

8
votes

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.