简体   繁体   中英

How do you reference objects created in another class when making arraylist?

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?

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM