views:

168

answers:

4
+1  Q: 

Java Inner Classes

im new to Java and have the following question regarding inner classes:

when implementing a inner class do i need to declare its attributes and methods scope i.e. public, private, protected?

EDIT: with the absense of delegates like in C# could someone mention how best to implement a messaging system in Java that enables communication between multiple forms (Jframe)?

i have read that i can use inner classes for this but then im also told i should not implement inner classes for than a few lines in size. which school should i follow?

+3  A: 

If you want to.

An inner class is roughly speaking like any other class. (Except that if you don't declare it static, it will have an Superclass.this reference.)

aioobe
Perhaps more like an EnclosingClass.this reference?
jjujuma
@jjujuma: of course, thanks.
aioobe
+2  A: 

I would suggest treating inner classes as private.

In Java, an outer class and all of its nested (including inner) classes can fiddle with each others privates. (The generated bytecode may be pointlessly verbose with additional synthetic access methods, but this is highly unlikely to matter.)

From an interface point of view, a class having weird inner class types is a bit weird. And more difficult to test if you are into that sort of thing. Too often nested type are created because creating a new file in a bad IDE is a bit of a pain - don't be tempted with nasty shortcuts.

Having said that inner classes are very useful. Use them with taste.

Tom Hawtin - tackline
+2  A: 

when implementing a inner class do i need to declare its attributes and methods scope i.e. public, private, protected?

It depends completely on how you wanted the inner class to behave. By default, an inner class is non-static:

public class Example1
{
    int a;

    public class Example2
    {
    int b;

    void test () {}
    }
}

A non-static inner class can be instantiated only inside a non-static method of the outer class. This is because every instance of a non-static inner class must be associated with an instance of the outer class. In a sense, every instance of a non-static inner class exists ``inside'' an instance of the outer class. A single instance of the outer class may have associated with it more than one instance of the inner class.

Because an instance of a non-static inner class has an associated instance of the outer class, the methods of the inner class can access directly any of the members (fields or methods) of the outer class instance. For example, the test method defined above can access both a and b directly

A class defined within another class is called a nested class. Like other members of a class, a nested class can be declared static or not. A nonstatic nested class is called an inner class. An instance of an inner class can exist only within an instance of its enclosing class and has access to its enclosing class's members even if they are declared private. The following table shows the types of nested classes:

Types of Nested Classes Type Scope Inner static nested class member no inner [non-static] class member yes local class local yes anonymous class only the point where it is defined yes

harigm
You beat me to it :)
Adam Gent
maybe you should give the source of the copied text: http://java.sun.com/docs/books/tutorial/java/javaOO/summarynested.html
echox
A: 

Although this is not an answer your question but make sure you are aware of the "static" modifier of inner classes.

public class Stuff {
public static class SubStuff {
    //private or protected
}
}

Is different than this:

public class Stuff {
public class SubStuff {
    //only private
}
}

If you have a static inner class than you might want protected variables, protected methods so on. But for inner classes that are not static generally you want everything private.

Google for the difference.

Adam Gent