Java JSP Application Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP  Java JSP Application Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Java JSP Application Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Java JSP Application Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Java JSP Application Hosting Web Hosting website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, JSP,Python
Java JSP Application Hosting Web Hosting, website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, JSP, Python Java JSP Application Hosting Web Hosting, website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, Python,JSP
Java JSP Application Hosting Web Hosting Sign-Up Java JSP Application Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting  Java JSP Application Hosting Resellers web hosting, website hosting, web site hosting Java JSP Application Hosting EZ Site Control Panel for web hosting,website hosting, web site hosting
Java JSP Application Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, Python,JSP,  Fundraising
Java JSP Application Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting
WWW.

Call Us Toll-Free
(877) 256-0328

Outside USA
1 - (201) 505-0430

Java JSP Application Hosting Welcome Java JSP Application Hosting Web Hosting Plans Overview , Fund Raising, Fundraising, web hosting, website hosting, web site hosting Java JSP Application Hosting Fund Raising, Fundraising, web hosting Java JSP Application Hosting Resellers, web Hosting Java JSP Application Hosting Web Design, web Hosting Java JSP Application Hosting Extra Services,  web Hosting Java JSP Application Hosting Traffic Booster, web hosting Java JSP Application Hosting Traffic Booster, web hosting Java JSP Application Hosting Technical Support,  web Hosting Java JSP Application Hosting webmaster tips,  web Hosting Java JSP Application Hosting 30 Day Money Back, web hosting Java JSP Application Hosting Legal Notices for Web Hosting Java JSP Application Hosting Glossary Computer Terms for web Hosting Java JSP Application Hosting Contact Information - web hosting

Site Map
Java JSP Application Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, Python, JSP Java JSP Application Hosting Java JSP Application Hosting Java JSP Application Hosting Java JSP Application Hosting Java JSP Application Hosting Inner Class Example (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Trail: Learning the Java Language
Lesson: Classes and Objects
Section: Nested Classes
Home Page > Learning the Java Language > Classes and Objects
Inner Class Example
To see an inner class in use, consider a simple stack of integers. Stacks, which are a common data structure in programming, are well-named— they are like a "stack" of dishes. When you add a dish to the stack, you put it on top; when you remove one, you remove it from the top. The acronym for this is LIFO (last in, first out). Dishes on the bottom of the stack may stay there quite a long time while the upper dishes come and go.

The StackOfInts class below is implemented as an array. When you add an integer (called "pushing"), it goes into the first available empty element. When you remove an integer (called "popping"), you remove the last integer in the array.

The StackOfInts class below (an application) consists of:

  • The StackOfInts outer class, which includes methods to push an integer onto the stack, pop an integer off the stack, and test to see if the stack is empty.
  • The StepThrough inner class, which is similar to a standard Java iterator. Iterators are used to step through a data structure and typically have methods to test for the last element, retrieve the current element, and move to the next element.
  • A main method that instantiates a StackOfInts array (stackOne) and fills it with integers (0, 2, 4, etc.), then instantiates a StepThrough object (iterator) and uses it to print out the contents of stackOne.
public class StackOfInts {
	
	private int[] stack;
	private int next = 0;  // index of last item in stack + 1
	
	public StackOfInts(int size) {
		//create an array large enough to hold the stack
		stack = new int[size];
	}
	
	public void push(int on) {
		if (next < stack.length)
		   stack[next++] = on;
	}
	public boolean isEmpty() {
		return (next == 0);
	}
	
	public int pop(){
		if (!isEmpty()) 
		   return stack[--next]; // top item on stack
		else
		   return 0;
	}
	
	public int getStackSize() {
		return next;
	}
	
	private class StepThrough { 		
		// start stepping through at i=0
		private int i = 0; 
		
		// increment index
		public void increment() {
			if ( i < stack.length)
			   i++;
		}
		
		// retrieve current element
		public int current() {
			return stack[i];
		}
		
		// last element on stack?
		public boolean isLast(){
			if (i == getStackSize() - 1)
			   return true;
			else
			   return false;
		}
	}
	
	public StepThrough stepThrough() {
		return new StepThrough();
	}
	
	public static void main(String[] args) {
		
		// instantiate outer class as "stackOne"
		StackOfInts stackOne = new StackOfInts(15);
		
		// populate stackOne
		for (int j = 0 ; j < 15 ; j++) {
			stackOne.push(2*j);
		}
		
		// instantiate inner class as "iterator"
		StepThrough iterator = stackOne.stepThrough();
		
		// print out stackOne[i], one per line
		while(!iterator.isLast()) {
			System.out.print(iterator.current() + " ");
			iterator.increment();
		}
		System.out.println();
		
	}
	
}
The output is:
0 2 4 6 8 10 12 14 16 18 20 22 24 26 
Note that the StepThrough class refers directly to the stack instance variable of StackOfInts.

Inner classes are used primarily to implement helper classes like the one shown in this example. If you plan on handling user-interface events, you'll need to know about using inner classes because the event-handling mechanism makes extensive use of them.

Local and Anonymous Inner Classes

There are two additional types of inner classes. You can declare an inner class within the body of a method. Such a class is known as a local inner class. You can also declare an inner class within the body of a method without naming it. These classes are known as anonymous inner classes. You will encounter such classes in advanced Java programming.

Modifiers

You can use the same modifiers for inner classes that you use for other members of the outer class. For example, you can use the access specifiers — private, public, and protected — to restrict access to inner classes, just as you do to other class members.
Previous page: Nested Classes
Next page: Summary of Nested Classes
 
 
 

Add to My Yahoo!

XML icon

Add to Google

 

 

 

 

 

 

 

 

 

 

 

JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP at JSP.aldenWEBhosting.com Servlets at servlets.aldenWEBhosting.com Tomcat at Tomcat.aldenWEBhosting.com mysql at mysql.aldenWEBhosting.com Java at Java.aldenWEBhosting.com Web Hosts Portal Web Links Web Links Web Hosting JSP Solutions Web Links JSP Solutions Web Hosting Servlets Solutions Web Links Servlets Solutions Web Hosting Web Links Web Links . . .
.
.
. .
.
. .
. . . . . . . . . . . jsp hosting servlets hosting web hosting web sites designed cheap web hosting web site hosting myspace web hosting