开发者

Java Inner Classes

I'm new to Java and have the following question regarding inner classes:

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

EDIT: With the absence of delegates (as 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 I'm also told I should not implement inner开发者_如何转开发 classes more than a few lines in size. Which school should I follow?


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 EnclosingClass.this reference.)


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.


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


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.


Inner can be seen by only the enclosing class. Its mostly used to achieve a utility function within the class. Lets use Door and Password as example.

public class Door {

public boolean isPassword(String key) {
    
    //local inner class - its hidden from the world
    class Password{
        public boolean isPassword(String key) {
            if(!key.equals("xyz")) {
                System.out.println("Not Password. Door is locked");
                return false;
            }else {
                System.out.println("Password corect. Door is opened");
            return true;
            }
        }
    }
    return new Password().isPassword(key);
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜