Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-03-2007, 03:20 PM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
When do we use inner classes?
Anybody could help?
Im still a bit confused with the use of inner class
What do we use inner class for actually and why should we use inner class?

Declaring a class inside another still seems bit weird for me, don't get it

Thanks

CruxBlack
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-03-2007, 04:21 PM
Member
 
Join Date: Jul 2007
Posts: 41
gabriel is on a distinguished road
maybe with this example you can understand it:

Consider a simple stack of integer.
When you add a integer to the stack, you put it on top; when you remove one, you remove it from the top.

The IntegerStack class below is implemented as an array.

When you add an integer, it goes into the first available empty element. When you remove an integer, you remove the last integer in the array.

The IntegerStack class below consists of:

* The IntegerStack 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 IntegerStack 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.
Code:
public class IntegerStack { private int[] stack; private int next = 0; // index of last item in stack + 1 public IntegerStack (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" IntegerStack integerStack = new IntegerStack (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 IntegerStack .

Inner classes are used primarily to implement helper classes like the one shown in this example.

I hope that you have understood the idea, I searched this example on google.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-03-2007, 04:24 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
One advantage i can think of is that if you don't want to document a class and want to hide it from outside world, you can use it in that way

And actually i don't think there is any strict rule for making a class inner. So it is more likely a choice matter IMO!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-03-2007, 04:28 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
And i think gabriel's example is very clear. So, they are helper classes but if you want to help others, you can make those classes public!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-04-2007, 04:43 AM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
Hmm..so it's a helper class?
To hide it from another class, that i get, analogically, it's like having a class that are owned by it's outer class only right?
But then, won't this class capabilities be limited and un-reusable?
Why not create an instance of another class in that class instead then, doesn't that give the same effect?

Btw, is there any limit on how deep could an inner class be?
Or maybe it have no limit and could go on like, A is the inner class of B which are the inner class of C in which are the inner class of D and so on and so on...
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-10-2007, 06:00 PM
Senior Member
 
Join Date: Jun 2007
Posts: 164
Heather is on a distinguished road
the advantage of inner class is that this class can access to the methods of the container class
It useful to do listeners classes

these classes are anonymous, that is to say, they don't have name and they don't assign to a variable.

it's the same have a class that are owned by it's outer class
But I don't know why people doesn't do that

Quote:
won't this class capabilities be limited and un-reusable
YES

Quote:
is there any limit on how deep could an inner class be?
yes

Last edited by Heather : 08-10-2007 at 06:21 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes in graphics CyberFrog New To Java 0 04-02-2008 10:11 PM
using elements from other classes Camden New To Java 1 03-21-2008 08:25 AM
Need help with importing classes Deathmonger New To Java 3 02-07-2008 11:03 AM
Using a JAR from other classes Joe2003 Advanced Java 1 01-02-2008 08:08 PM
EJB, classes Model Felissa Enterprise JavaBeans 1 07-06-2007 04:17 PM


All times are GMT +3. The time now is 05:41 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org