This discussion is archived
10 Replies Latest reply: Dec 18, 2007 5:13 PM by 807601 RSS

Inner classes

807600 Newbie
Currently Being Moderated
I'm trying to make sense out of the need for an Inner Class; can variables be used instead of an Inner class? what functionality does the inner class offer that an instance variable/class methods cannot offer? Please refer to code fragment below when you respond?
thanks.

class Animal{

//why not String brainCharacteristics;

instead of
class Brain{
//code
}//end inner class

}//end class Animal
  • 1. Re: Inner classes
    807600 Newbie
    Currently Being Moderated
    Inner classes are not technically necessary (you can do anything without them that you could do with them) but are very very handy

    First of all, there are two kinds of inner classes - static and non-static

    here's an example of a static inner class from my EditableList class, a dialog used to pop up the list. While you could put this in its own file, putting it in the same file as EditableList helps with documentation (others can read the class and see it) and helps to keep the two classes more closely knit.
         public static class ELStandardDialog extends StandardDialog {
              private EditableList mList;
              public ELStandardDialog(Object[] listItems) {
                   super();
                   mList = new EditableList(listItems);
                   setMainContent(mList);
              }
              public void apply() {}
              public boolean showApplyButton() { return false;}
              public EditableList getEditableList() {
                   return mList;
              }
         }
    The other type of inner class is the non-static type. This is more important and used more often than static inner classes. An example:
    ...
    JButton b = new JButton("Hello");
    b.addActionListener(new ActionListener() { // created an anonymous inner class
    }
    the question of 'why do you need it?' inner classes keep code cleaner, help keep related classes together and make it easier for others to see how the system works.
  • 2. Re: Inner classes
    807600 Newbie
    Currently Being Moderated
    Thank you for your input sir; so the bottom line is: "handiness & code clarity" versus "necessity".
  • 3. Re: Inner classes
    807600 Newbie
    Currently Being Moderated
    Basically, that's right. It's more art than science to say what classes should be in the same package and which classes are so tightly coupled with another class that they should be inner classes. If you continue to program, you will encounter this more yourself
  • 4. Re: Inner classes
    807600 Newbie
    Currently Being Moderated
    Also (I can't believe I forgot to add this) inner classes, especial non-static inner classes, provide benefits to type safety. For more on the concept of type safety, search the web
  • 5. Re: Inner classes
    807600 Newbie
    Currently Being Moderated
    thanks TJacobs
  • 6. Re: Inner classes
    807600 Newbie
    Currently Being Moderated
    Also, anonymous inner classes are good for classes (like the ActionListener example above) that must not be called outside your enclosing class.
  • 7. Re: Inner classes
    807601 Newbie
    Currently Being Moderated
    Hello everyone.

    I just want to ask bout this inner class thing.

    Is it allright to make an inner classes more than one, let's said bout twenty of it?

    And how much is the limit size of a java class that u can make?
    Because i'm afraid that my class can be too large because it have a lot of inner class in it.

    Thx.
  • 8. Re: Inner classes
    800308 Newbie
    Currently Being Moderated
    Is it allright to make an inner classes more than one, let's said bout twenty of it?
    What?

    And how much is the limit size of a java class that u can make?
    Because i'm afraid that my class can be too large because it
    have a lot of inner class in it.
    What? Again, I don't understand the question.

    Hmmm.. At face value it sounds like a bad design... I'm guessing that your design is a lot more complicated than it needs to be, and is therefore inflexible, and is therefore unlikely to be reusable, and therefore destined for the trash bin.
  • 9. Re: Inner classes
    807601 Newbie
    Currently Being Moderated
    Ups, i'm sorry.
    Please forgive me for didn't state my question clear.

    Well, right now i'm making a chess progr ( AI vs HUMAN ) and in one of the class( class BoardCanvas ), i want to define quite many inner class for building the AI of the BLACK piece.

    I read in some thread about inner class in this forum, that said that if some of your class( for ex: class B,C,D ) didn't used outside one class that used it( for ex: class A ) then defined it as inner class in class A.

    And for me itself, it's more clearer and helpful also for making the AI classes become inner classes.
    Because there are some methods in the outer class( BoardCanvas class ) that being needed in the inner classes. So by making the AI as inner classes, i didn't need to make object of the outer class in order to access those methods.

    The problem is i'm afraid that my class goes too big because of the AI inner class.
    Right now my BoardCanvas.java has become 52 KB when i compile it and has gone through 1965 lines. And till now i have put 9 inner class in it.
    I still want to add about 11 inner classes.

    So my question is:
    How much exactly is the limit size of a code or a java file in java, is it the 64 kb like method?
    I hope i make my question clearer now and if i didn't pls forgive me.

    Lots of thanx to you all.
  • 10. Re: Inner classes
    807601 Newbie
    Currently Being Moderated
    Hello everyone. I just want to say that my question has been answered.
    Sorry for asking the silly question and taking your precious time.

    Well it is definitely means that i'm still need a lot of work to learn bout Java.
    Honestly I'm gratefull to be in this forum because the people in this forum are always kind and helpfull to people like me who are new to Java.

    Thx for all.