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.

2
  • There is no inner class here. There is a static nested class. Not the same thing. Commented Dec 24, 2014 at 7: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 . Commented Dec 24, 2014 at 8:08

1 Answer 1

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.

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

12 Comments

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 Well the book[] array should remain static. You should rename that variable though. It might conflict with the class name.
@Eran book.book[50] = new GuiApp.book(); I don't think it is required?
@eatSleepCode If you don't do it, book.book[50] will remain null and book.book[50].personName will throw a NullPointerException.
@Eran your accessing a static field which don't require object of the class to access it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.