0

I have 2 classes which I am making in blueJ- one called User and the other UserGroup. My user class looks like this:

public class User{
     public enum UserType{                          
        ADMIN, EDITOR, USER;
     }

     public String id;                            
     public UserType userPermissions;              
     public String actualName;                      

     public User(String username, UserType userType, String name){
         id = username;
         userPermissions = userType;
         actualName= name;
     }

     public String getUsername(){
         return id;
     }

     public UserType getUserType(){
         return userPermissions;
     }       

     public String getName(){
         return actualName;
     }

     public void setUserType(UserType input){
         userPermissions = input;
     }
}

And my UserGroup class like this so far:

import java.util.*;

public class UserGroup{

    private static ArrayList<User> UserGroup= new ArrayList<User>();

    public static void add(String username, UserType userType, String name){
        UserGroup.add(new User(username, userType,name));
    }


}

My problem is when I try to compile UserGroup it says "cannot find symbol- class UserType". I am trying to create an arraylist in UserGroup of Users created in the User class. Where am I going wrong?

2 Answers 2

1

UserType is an inner class of User:

public class User {

    // inner

    public enum UserType {
        ADMIN, EDITOR, USER;
    }

If you want to use it in UserGroup there are two things you can do. First you can import it and then you can access it from UserGroup:

import mypackage.User.UserType;

public class UserGroup {

Or without importing it you can access it from UserGroup with a dot:

User.UserType type = User.UserType.ADMIN;

You could put it in its own class but in my opinion making it an inner in User makes sense because it is innately an attribute of User.

Also one thing to note is that normally the rules for inner classes are a bit different than regular classes. Normally inner classes are instance members, that is the inner class is owned by the instances of the outer class, not the outer class itself. To access an inner class like Outer.Inner you have to declare the inner class as static. Enums are static by default so you don't have to declare them as such.

Sign up to request clarification or add additional context in comments.

3 Comments

this may seem like a bit of a stupid question but what would my mypackage be called?
I have tried import package.User.UserType; and it said identifier expected
Whatever you've named the package. If you aren't declaring a package (as in package mypackage;) try using it with the dot as in my second example. You can't import from an unnamed package.
0

The enum UserType must be a separate class; at the moment it's an inner class accessible to User class only and it is not accessible to the UserGroup class. This can be achieved by using the IDE to create a new enum called UserType while removing e inner class enum that has been used within User.

Alternatively, you could use he import as has been mentioned:

import packagename.User.UserType;

2 Comments

I am trying to import but I can't firgure out how- I think my package is just called package. When I tried this it said 'identifier expected'. When I try mypackage.User.UserType; I get package.mypackage.User does not exist!
Where is your User class? Does it have a package declaration before: public class User { etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.