7 Replies Latest reply: May 11, 2009 11:10 AM by 807588 RSS

    Can I declare enum as a static ?Use of static .

    807588
      I want to declare my enumas a staic variable.

      public static enum userMode {Guest,RegisteredUser,Administrator};

      After login, according to user's creadentials ,User will get one of three defiened values.ie . user will falll in one of three categories (Guest,RegisteredUser,Administrator)...and depending on his authority I want to show him data . I am working on web application which will be accessed by lot many users at a time. So can I declare my enum as static as declared above ?

      I think I can't because for each user this "userMode" will be different,and if I made it as static then, one user may get value of other user.

      Could you make clear my confution?
        • 1. Re: Can I declare enum as a static ?Use of static .
          807588
          Enum variables are static by default. Here's an example of how you could use it.
          enum UserTypes {GUEST, ADMIN, REGISTEREDUSER}
          
          class User {
              
              private UserTypes userType;
              
              public User() {
                  
              }
              
              public void login(String parameter) {
                  //Do some stuff
                  this.userType = UserTypes.ADMIN; 
              }
          }
          Edit: Also, read this. [http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html]

          Edited by: codingMonkey on 2009/05/11 11:07
          • 2. Re: Can I declare enum as a static ?Use of static .
            807588
            Hi,
            There will be one servlet for each url pattern in a web application. So when a new request comes the servlet's service() method gets called which allocates a new thread for handling request. So each user have separate threads.
            If you have declared any method as not synchronized then these threads can change values in these methods. Use synchronized methods or synchronized blocks to avoid this situation.
            There is no need to declare the enum as static.
            Regards..
            • 3. what do we mean by static
              807588
              what is the meaning of static and where it is used
              • 4. Re: what do we mean by static
                Puce
                http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
                http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html

                If I remember correctly it's not possible to define an "inner enum" (non-static nested enum) -> enums (type definition) are always static
                • 5. Re: what do we mean by static
                  807588
                  static members of a class doesn't belong to any specific instance(object) of the class. It is same for all the instances of the class. It is used when some value is not dependent on any specific instance of the class.
                  The static is for the whole class(independent of instance).
                  • 6. Re: what do we mean by static
                    Puce
                    programmer1985 wrote:
                    static members of a class doesn't belong to any specific instance(object) of the class. It is same for all the instances of the class. It is used when some value is not dependent on any specific instance of the class.
                    The static is for the whole class(independent of instance).
                    The static keyword on type (especially class) level has different semantics:
                    http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
                    • 7. Re: Can I declare enum as a static ?Use of static .
                      807588
                      Archana144 wrote:
                      Could you make clear my confution?
                      Your confution, er, confusion, seems to center around confusing the definition of a type (UserMode) with the definition of a field of that type. These are two different things, right? I can define a type, say a linked list class, and I can define a field with is a linked list of snacks I enjoy, and there is a fundamental difference between the two.