Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Tolls

Thread: variables in controller class --> become static

  1. #1
    rchiu5hk is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default variables in controller class --> become static

    Dear all,

    Previously I was using spring MVC and struts2. I experienced that variables in controller class become static which I means it is same to all accessed users. I mean if I input information in page A and then go to page B, another user go to page A which will show my inputted information. Sorry for my stupid. As in my sense, i think it should only be session and only available to me. If so, then I need to make all variables become session variables if I want only visible to me. Am I correct? Just want to make sure this for my afterwards programming.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: variables in controller class --> become static

    Static variables are class variables and for any given variable, all users accessing the class see the same value. For instance variables (more properly referred to as instance fields) each instance of the class has its own value per instance variable.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: variables in controller class --> become static

    They don't "become static". What's happening is that there is a single instance of a controller in the application that all requests use, which is why it is not recommended to give any of these things state except in very specific circumstances.

    So, yes, you should either be storing stuff in the session, if it's not too much data (and what "too much" means will depend entirely on your application), or it should be saved to some persistence framework, eg a database.
    benji2505 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: variables in controller class --> become static

    Default scope of a Spring bean is singleton.

Similar Threads

  1. Replies: 2
    Last Post: 05-14-2016, 11:45 PM
  2. Replies: 6
    Last Post: 02-15-2010, 04:31 AM
  3. Replies: 4
    Last Post: 10-22-2009, 05:45 PM
  4. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  5. Help with static variables
    By bbq in forum Advanced Java
    Replies: 1
    Last Post: 06-28-2007, 05:38 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •