简体   繁体   中英

Can't access public member of public static class inside public class (all in a separate jar)

Well, that's a mouthful of a title. It gets my point across, though. Here's the gist of my code, inside the jar:

public class NetworkShared {
    public static class LoginRequest {
        public String Username;
        //...

Then, to access it, I'm doing something like this:

NetworkShared.LoginRequest request = new NetworkShared.LoginRequest();
request.Username = "example"; //this is the problem line

It's when I try to access request.Username that I have a problem. Eclipse says the field NetworkShared.LoginRequest.Username is not visible. I'm puzzled because it's public all the way down. Java's not my main language, so I might be missing something. Does anyone know?

EDIT: I might add that this on Android. NetworkShared is in its own JAR and I've added it to the build path. Nothing else is wrong except for accessing request.Username.

Conclusion: Seems like this was an Eclipse refresh problem with a referenced JAR.

It should work. A top-level class is implicitly static so basically NetworkShared.LoginRequest is a correct way to identify the LoginRequest class. You are doing it the right way.

My answer is not to your eclipse problem -- it is to your Java question -- so at least you can get your Java related worries out of the way.

Outside the fact that your eclipse might have a bug (would not the first either), could you please compile the following code in your eclipse? You might have trimmed down your original code to something that actually works.

NetworkShared.java

public class NetworkShared {
    public static class LoginRequest {
        public String Username;
    }
}

Test.java

public class Test {
    public static void main(String[] args) {
        NetworkShared.LoginRequest o = new NetworkShared.LoginRequest();
        o.Username = null;
    }
}

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