-3

How can one store & access attributes in the following way in Java?

InnerTest it = new InnerTest();
it.id = "123";
it.user.userID = "456";

The following code does not do what is desired, but it offers a backdrop for the question:

public class InnerTest {
    public String id;
    static class user {
        static String userName;
        static String userID;
    }
}

In case my approach is way off, the exercise is to store a JSON architecture in a class, but access it simply as given above. I am creating a class for Tweets, and one inner group of attributes is under "user":

[...]
 "in_reply_to_user_id" : null,
  "in_reply_to_user_id_str" : null,
  "in_reply_to_screen_name" : null,
  "user" : {
    "id" : 19792676,
    "id_str" : "19792676",
    "name" : "(((Payne¯\\_(ツ)_/¯)))",
[...]

I am looking for a way to elegantly represent in my class' use those inner variables, as in:

foo.user.id_str ...

Is a type of nested/anonymous class correct? It doesn't look like it is-even with static modifiers...

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Yumi Koizumi
  • 331
  • 2
  • 4
  • 12
  • That is a nested class, not an inner class. Also, why are the member variables static at all? Your JSON seems to indicate that a `InnerTest` should have a field of type `user`. – Sotirios Delimanolis Jun 19 '16 at 15:06
  • @SotiriosDelimanolis, like I said, the class I was playing with in scratch was just a backdrop for the question. Thanks for the edits... – Yumi Koizumi Jun 20 '16 at 01:12
  • If you are serializing with GSON, I recommend you [@SerializedName](https://google.github.io/gson/apidocs/com/google/gson/annotations/SerializedName.html) annotation. You won't have to use the underscored names as your variable names. – Jezor Jun 20 '16 at 02:50

2 Answers2

1

Nested class is a good choice in this situation. However, you misunderstood the purpose of static in a static class - this is a syntax for creating classes that can be created independently of the instance of their outer class. It is not a class that must have only static fields and methods.

Therefore, your declaration should be fixed to make userName and userId fields non-static:

public class InnerTest {
    public String id;
    public UserType user = new UserType();
    static class UserType {
        String userName;
        String userID;
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • thanks for writing. I appreciate the correction, but I may be missing how to access these inner member from the outer instance, a la: ` InnerTest it = new InnerTest(); it.id = "123"; it.user.userName = "456";` ...where all variables, however nested, can be accessed via a minimalistic way, such as: it.user.userID, etc. Is that simply not possible? – Yumi Koizumi Jun 20 '16 at 01:25
  • @YumiKoizumi You need to create a field called `user` for the nested type. Please see the edit. – Sergey Kalinichenko Jun 20 '16 at 01:33
  • @dasblinkinlight ...Perfect! Really great! I was beginning to think Java couldn't do it! Thank you! :) – Yumi Koizumi Jun 20 '16 at 01:45
1

Your nested class definition have static fields, which are bound to a class rather than to objects. If we change them to public, the definition will be correct, although you still didn't add a member variable of this class inside the InnerTest class. Take a look at this answer, it clarifies the difference between class definition and object instance.

public class InnerTest {
    public String id;
    public User user;

    static class User {
        public String userName;
        public String userID;
    }
}

Now you can create a new InnerTest object and access user data the way you intended.

Community
  • 1
  • 1
Jezor
  • 3,253
  • 2
  • 19
  • 43
  • I am sorry to not have made clearer the way I intended. I intended to access inner variables from the same instance, avoiding making many instances to represent a single class (at the top). This is why `it.user.userID = "456";` is desired. Representing a JSON tweet, which has a ton of nested structures, would get messy very fast with instances of all inner classes. Hopefully java has a way to do this simply... – Yumi Koizumi Jun 20 '16 at 01:30