Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Aphorismen
Applications
Business Economics & Admin.
My Computers
Cooking
Devices
Folders
Food
Hardware
Infos
Software Development
Sports
Operation Instructions
Todos
Test
Help
Glossary
Community portal
adaptions
Sidebar anpassen
Wiki RB4
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
JavaNestedClasses
Page
Discussion
English
Read
Edit
View history
Toolbox
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
MovedToGoogleWiki ==Inner Classes== An inner class in contrast to a top level class is a class that is defined inside another class. Inner classes can access everything of the outer class. Inner classes are the only classes that can be private. Es gibt vier Arten von inner classes: *statische innere Klassen (nested top-level class) *Elementklasse (member class) *Lokale Klassen (in Methoden) *Anonyme innere Klassen Inner classes are handled by the compiler. The virtual machine does not know anything about it. The name of the inner class is <OuterClassName>$<InnerClassName> and there are normal class files generated prefixed with an $. ===Static inner classes=== Static inner classes or nested top level classes are declared and used like this: public class Lamp { static String s = "hi"; int i = 1; static class Bulp { void printHi() { System.out.println( s ); // System.out.println( i ); // error, because i is not static } } public static void main(String[] args) { Bulp bulp = new Lamp.Bulp(); bulp.printHi(); } } There is no difference between a top level class and a nested top level class. You don't need an object of the outer class to use it. ===Member classes=== Declaration and instantiation: public class Frame { String s = "hi"; class Pattern { void printHi() { System.out.println(Frame.this.s); } } } Frame f = new Frame; Frame.Pattern p1 = f.new Pattern(); Frame.Pattern p2 = new Frame().new Pattern(); The member class can access everything, even the private member, of the outer class. There must be an object of the outer class to create an inner class object. The inner class may not have any static properties. ===Local classes=== Local classes are declared as an assignment. There is no access modifier for the class or methods or any static property. A local class can access every method of the outer class and any final attributes. ===Anonymous classes=== An anonymous class behaves just like a local class, and is distinguished from a local class merely in the syntax used to define and instantiate it. Anonymous classes can not have an ctor because the ctor must have the same name as the class and this class doesnโt have a name. There is another feature, accessing parameters, which is very special and not recommended. Die Declaration erfolgt durch: new <Classname>(<optional argument list>) { <ClassBody> } An popular example of such an anonymous class is an implementation of an predefined adapter for event handling: AddMouseListener(new MouseAdapter() { public mousePressed() { ... } } );
Summary:
Please note that all contributions to Wiki RB4 may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Uwe Heuer Wiki New:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width