Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Global Variable - Problem in diff. session.

Global Variable - Problem in diff. session.

Scheduled Pinned Locked Moved Java
helptutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nanda_MR
    wrote on last edited by
    #1

    Hi to all,

    public class xyz {

    public static int gUserID = 1;

    }

    When 1st user Login gUserID value is = 5 and second user login gUserID value is = 7 Problem is 1st user refresh or re-direct to another page gUserID is changing to 7. How to set Global variable for session or any solution to above problem. Thanks.

    N J 2 Replies Last reply
    0
    • N Nanda_MR

      Hi to all,

      public class xyz {

      public static int gUserID = 1;

      }

      When 1st user Login gUserID value is = 5 and second user login gUserID value is = 7 Problem is 1st user refresh or re-direct to another page gUserID is changing to 7. How to set Global variable for session or any solution to above problem. Thanks.

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #2

      How is the variable being updated? The single variable is used by every instance of the class. If you want each instance to have a userId then you need something like this:

      public class Test {
      // note it's private! This stop anything else from changing it.
      private static int nextUserId = 1;

      private int userId;
      public Test() {
          this.userId = Test.nextUserId++;
      }
      
      public int getUserId() {
          return this.userId;
      }
      

      }


      Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

      N 1 Reply Last reply
      0
      • N Nagy Vilmos

        How is the variable being updated? The single variable is used by every instance of the class. If you want each instance to have a userId then you need something like this:

        public class Test {
        // note it's private! This stop anything else from changing it.
        private static int nextUserId = 1;

        private int userId;
        public Test() {
            this.userId = Test.nextUserId++;
        }
        
        public int getUserId() {
            return this.userId;
        }
        

        }


        Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

        N Offline
        N Offline
        Nanda_MR
        wrote on last edited by
        #3

        Hi Nagy Vilmos, I m update gUserID from database. As soon as login to application. I m setting corresponding user id to that variable and I m utilizing that variable in complete project as global variable. here I m facing problem. As soon as 2nd person login The Value is reset (2nd user id is updating). I m trying your method now Thanks.

        N 1 Reply Last reply
        0
        • N Nanda_MR

          Hi Nagy Vilmos, I m update gUserID from database. As soon as login to application. I m setting corresponding user id to that variable and I m utilizing that variable in complete project as global variable. here I m facing problem. As soon as 2nd person login The Value is reset (2nd user id is updating). I m trying your method now Thanks.

          N Offline
          N Offline
          Nagy Vilmos
          wrote on last edited by
          #4

          Now I understand. You have an instance for each user, so the variable must not be static. If it is possible to set the value in the constructor, then that is the best thing because you can make the member variable final and so it cannot be changed. It is strongly advised to not expose member variables outside the class, it is ALWAYS better to use in this case a method; as I used in my example. If the variable is final, then you can to some extent relax the rule as there is no way it can be changed; but this is normally reserved for constants.


          Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

          T 1 Reply Last reply
          0
          • N Nagy Vilmos

            Now I understand. You have an instance for each user, so the variable must not be static. If it is possible to set the value in the constructor, then that is the best thing because you can make the member variable final and so it cannot be changed. It is strongly advised to not expose member variables outside the class, it is ALWAYS better to use in this case a method; as I used in my example. If the variable is final, then you can to some extent relax the rule as there is no way it can be changed; but this is normally reserved for constants.


            Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

            T Offline
            T Offline
            TorstenH
            wrote on last edited by
            #5

            my +5. I would like to point out that the UserID should also be an automatically created number that the user object should not modify - it's hard to identify the data otherwise.

            regards Torsten When I'm not working

            1 Reply Last reply
            0
            • N Nanda_MR

              Hi to all,

              public class xyz {

              public static int gUserID = 1;

              }

              When 1st user Login gUserID value is = 5 and second user login gUserID value is = 7 Problem is 1st user refresh or re-direct to another page gUserID is changing to 7. How to set Global variable for session or any solution to above problem. Thanks.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Nanda_MR wrote:

              How to set Global variable for session

              You start by researching how to handle session data in a web application.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups