120 likes | 486 Views
Inner Classes. An Inner Class is a class defined inside of another class (Static) Inner Classes are a lot like C++ ’ s nested classes Since Java 1.1, Java now has Inner Classes
E N D
Inner Classes • An Inner Class is a class defined inside of another class • (Static) Inner Classes are a lot like C++’s nested classes • Since Java 1.1, Java now has Inner Classes • Originally, the Java authors decided not to include them because they thought they would needlessly complicate the language. • However, they can be very useful, especially with AWT (GUI) event handling.
Inner Classes • An Inner class is a class defined inside of another class. Why would anyone want to do that? • An Inner class object can access the implementation of the object that created it – including private fields • Inner classes can be hidden from other classes in the same package • Anonymous inner classes are frequently used for creating event callbacks
How to declare an inner class • You declare an inner class almost like a method inside of a class class ICExample { private class thisIsAnInnerClass //THIS IS THE INNER CLASS { // define a class here } }
Visibility Rules • Inner classes are members of the outer class • Can be declared with any visibility (public, private, etc.) • Can see all members (including private) of outer class • Is in the same package as outer class (outer class can access all non-private inner class members) • Typically, the inner class is declared private and the inner classes members are either public or package visible.
outer keyword • With inner classes, Java defines a outer class reference with each inner class which can be accessed with the outer keyword //this is from the BankAccount class on page 216 of Core Java volume I public void actionPerformed(ActionEvent e) //inside the inner class { double interest=outer.balance * this.interestRate; outer.balance += interest; } • The balance field is a private member of the outer class.
Static inner classes • Objects of a static inner class can exist even without an object of the enclosing outer class. • Because of this, a static inner class is not allowed to access fields of the outer object
Local inner classes • You can actually declare an inner class inside of a method, just like you could declare a local variable. • Local classes do not get an access modifier – they are automatically restricted to the method they are defined in • Can only refer to final members of the enclosing class
Anonymous Inner classes • When using a local inner class, if you only want to make one instance of it, you don’t even need to give it a name • This is known as an anonymous inner class • These are convenient for event programming • However, the syntax is extremely cryptic. Look at this example
Anonymous Inner classes public void start(final double rate) { ActionListener adder = new ActionListener() { public void actionPerformed(ActionEvent evt) { double interest = balance * rate / 100; balance += interest; } }; Timer t = new Timer(1000, adder); t.start(); • This is saying, construct a new object of a class that implements the ActionListener interface, where the one required method (actionPerformed) is defined inside the brackets.
Anonymous Inner classes • You have to look very carefully to see a difference between construction of a new object, and construction of a new inner class extending a class. //A person object Person queen=new Person(“Mary”); //Person Object //An object of an inner class extending Person Person count = new Person(“Dracula”) { //class code here};
Anonymous Inner classes • Anonymous Inner classes cannot have constructors, since constructors have to have the same name as the class, and these classes have no names. • As you can see, the syntax for these is confusing – both for people writing and reading the code. Use this with care, if at all.