1

I want to refer to the variable of an object of a static nested class from the outer class. Would this work?

public class GuiApp {
    static class book{

        static book [] book = new book[1000];
        static Boolean overdue;
        static Boolean checkedOut;
        static int bookNum;
        static String personName;
        static String dueDate;
        static int month;
        static int date;
        static int year;
        static String dateCheckedOut;
    }
}

and later to refer to the variable String personName of book[50] from the outer class for example

book.book[50].personName = "Bob";

I'm not sure if I'm understanding this correctly so I want to know if this would work.

user207421
  • 305,947
  • 44
  • 307
  • 483
noobProgrammer
  • 467
  • 3
  • 8
  • 20
  • There is no inner class here. There is a static nested class. Not the same thing. – user207421 Dec 24 '14 at 07:13
  • Your class name should start with Capital Case and never use same name for two thing. Consdure change name of variable book to arrBook or books . – Panther Dec 24 '14 at 08:08

1 Answers1

1

Before accessing an element of that array :

book.book[50].personName = "Bob";

You have to initialize that element :

book.book[50] = new GuiApp.book();

I would also advise against using the same name for the class and the array.

However, making all the properties of the book class static makes no sense, since it means that all the books would have the same values.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Oh I just took out the static keyword but now I cannot refer to the variables and book objects in the outer class. W – noobProgrammer Dec 24 '14 at 06:56
  • @noobProgrammer Well the book[] array should remain static. You should rename that variable though. It might conflict with the class name. – Eran Dec 24 '14 at 06:58
  • @Eran `book.book[50] = new GuiApp.book();` I don't think it is required? – eatSleepCode Dec 24 '14 at 07:28
  • @eatSleepCode If you don't do it, book.book[50] will remain null and `book.book[50].personName` will throw a NullPointerException. – Eran Dec 24 '14 at 07:30
  • @Eran your accessing a static field which don't require object of the class to access it. – eatSleepCode Dec 24 '14 at 07:31
  • you can refer this question http://stackoverflow.com/questions/21255684/why-not-a-nullpointerexception-while-accessing-static-with-null-reference – eatSleepCode Dec 24 '14 at 07:32
  • @eatSleepCode your sample is different, as the accesses i is static, so the access getTest().i does not need to reference the return value but access the static class member i. – Jens-Peter Haack Dec 24 '14 at 07:36
  • @eatSleepCode `book.book[50]` is a reference of type book which contains null when the array is initialized. Even if personName stays static (which is a bad idea, as mentioned in my answer), dereferencing a null reference always leads to a NullPointerException – Eran Dec 24 '14 at 07:37
  • @Jens-PeterHaack here `static String personName;` too a static variable, so to my opinion this seems to the same case – eatSleepCode Dec 24 '14 at 07:44
  • @Eran `Session1.book.book[50].personName = "bob"; System.out.println("" + Session1.book.book[50].personName );` This code works fine in my case. – eatSleepCode Dec 24 '14 at 07:45
  • @eatSleepCode OK, I tested it and you were right. Accessing a static variable via an instance doesn't throw NullPointerException even if the instance is null. I guess the compiler replaces the reference with the class name, so book.book[50].personName becomes book.personName. That said, personName shouldn't be static in the first place. – Eran Dec 24 '14 at 07:49
  • So the primary problem is that the members are declared static. If they are not static it will work... and then the reference needs an initialized book object ;-)... eatSleepCode you are completely right. – Jens-Peter Haack Dec 24 '14 at 07:52