Inner classes: javaInner 1/4 -- Static member classes; don't require an instance, but can only access static members. -- Non-static member classes; can access any members, but require an instance. -- Local classes: a class declared inside a block (typically a method); static or not, according to the enclosing block (e.g. method).

mammetlizardlickSoftware and s/w Development

Nov 3, 2013 (4 years and 9 days ago)

153 views

Inner classes:










javaInner 1/4


--

Static member classes; don't require an instance, but can only access static members.


--

Non
-
static member classes; can access any members, but require an instance.


--

Local classes: a class declared inside

a block (typically a method);


static or not, according to the enclosing block (e.g. method).


--

Anonymous classes: a block implicitly declared as a class by being supplied


as an initializer to a class or interface which it extends or inherits
.


Anonymous classes do not have names, but can have initializing blocks


(like a no
-
arg constructor).


Initializing blocks:


public class Init {


public Init(){


System.out.println( "Constructor." );


}


{


System.out.println(

"Member block." );


}


public static void main( String[] args ){


Init z = new Init();


}

}


//
---------------------------

Static member class
---------------------------

import java.awt.event.*;

import javax.swing.*;


public class WinStat
icC {


private static class WinHandler extends WindowAdapter {


public void windowClosing( WindowEvent event ){


System.exit(0);


}


}



public static void main( String[] args )


throws In
terruptedException {


JFrame jFrame = new JFrame( "Jpeg or GIF Image" );


JLabel jLabel = new JLabel( new ImageIcon( args[0] ) );



jFrame.addWindowListener( new WinHandler() );


jFrame.getContentPane().add( jLabel );


jF
rame.setLocation(50, 50);


jFrame.pack();


jFrame.setVisible( true );


}

}













javaInner 2/4

Definition:

A nested class is a class that is a member of another class.


Definition:

An inner class is a nested class whose instance e
xists within

an instance of its enclosing class and has direct access to the instance

members of its enclosing instance.


//
-------------------------

Local inner class
-------------------------------


import java.awt.event.*;

import javax.swing.*;


public
class WinLocalC {


public static void main( String[] args ) throws InterruptedException {


JFrame jFrame = new JFrame( "Jpeg or GIF Image" );


JLabel jLabel = new JLabel( new ImageIcon( args[ 0 ] ) );


class WinHandler extends Windo
wAdapter {


public void windowClosing( WindowEvent event ){


System.exit(0);


}


}



jFrame.addWindowListener( new WinHandler() );


jFrame.getContentPane().add( jLabel );


jFrame.setLocation(

50, 50 );


jFrame.pack();


jFrame.setVisible( true );


}

}



//
----------------------

Anonymous inner class
------------------------------


import java.awt.event.*;

import javax.swing.*;


public class WinAnonC {


public static void mai
n( String[] args )


throws InterruptedException {


JFrame jFrame = new JFrame( "Jpeg or GIF Image" );


JLabel jLabel = new JLabel( new ImageIcon( args[0] ) );


jFrame.addWindowListener( new Window
Adapter() {


public void windowClosing( WindowEvent event ){


System.exit(0);


}


} );



jFrame.getContentPane().add( jLabel );


jFrame.setLocation(50, 50);


jFrame.pack();


jFrame.set
Visible( true );


}

}

Inner Classes










javaInner 3/4


To help you get a handle on inner classes and what they are good for, revisit the
Stack class. Suppose you want to add a feature to this class that lets another

class enumerate over the eleme
nts in the stack using the interface defined in
java.util.Enumeration. This interface contains two method declarations:



public boolean hasMoreElements();


public Object nextElement();


The Enumeration interface defines the interface for a single l
oop over the elements:



while (hasMoreElements())


nextElement()


If Stack implemented the Enumeration interface itself, you could not restart the loop
and you could not enumerate the contents more than once. Also, you couldn't allow
two enumer
ations to happen simultaneously. So Stack shouldn't implement
Enumeration. Rather, a helper class should do the work for Stack.


The helper class must have access to the Stack's elements.

It also must be able to access them directly because the

Stack's p
ublic interface supports only last
-
in
-
first
-
out access.

This is where inner classes come in.


Here's an implementation of Stack that defines a helper class

(called an adapter class) for enumerating over its elements:


public class Stack {


privat
e Vector items;



// ... code for Stack's methods and constructors not shown ...



public Enumeration enumerator() {


return new StackEnum();


}



class StackEnum implements Enumeration {


int lastItemPosit
ion = items.size()
-

1;


public boolean hasMoreElements() {


return (lastItemPosition >= 0);


}


public Object nextElement() {


if (!hasMoreElements())


throw new NoSuchEleme
ntException();


else


return items.elementAt(lastItemPosition
--
);


}


}


}













javaInner 4/4


Note that the StackEnum class refers directly to Stack's items instance variable.


Inner classes a
re used primarily to implement adapter classes like this example.

If you plan on handling events from the AWT or swing, adapter classes are

important to know, because the event
-
handling mechanism in the AWT/swing

makes extensive use of them.



Anonymous Cl
asses

You can declare an inner class without naming it.

Here's another version of the Stack class,

using an anonymous class for its enumerator:



public class Stack {


private Vector items;



...//code for Stack's methods and constructors
not shown...



public Enumeration enumerator() {


return new Enumeration() {


int lastItemPosition = items.size()
-

1;


public boolean hasMoreElements() {


return (lastItemPosition >= 0);



}


public Object nextElement() {


if (!hasMoreElements())


throw new NoSuchElementException();


else


return items.elementAt(lastItemPosition
-
-
);


}


}


}


}


Anonymous classes can make code difficult to read.

You should limit their use to those classes that are very small

(no more than a method or two) and whose use is well
-
understood

(like the AWT/swing even
t
-
handling adapter classes).