• Post Reply Bookmark Topic Watch Topic
  • New Topic

Purpose of Innerclass  RSS feed

 
srivijaya
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
What is the need of innerclass?I know that for protection from outsiders.We can do this by using private or protected modifiers.
Regards.
Vijaya.
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Innerclass is nested class. I think the main purpose is for clarity.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In some of their literature (eg the JLS), Sun define an inner class as a non-static nested class. I.e. an inner class would be one which carries an implicit reference to its "outer" parent.
Unfortunately, they did not use this definition consistently, which rather confuses the issue.
- Peter
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think people can use private or protected modifiers instead of inner class, but that will be a lot of confusion.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[DL]: Innerclass is nested class
All inner classes are nested classes. Not all nested classes are inner classes. If a class is nested and declared as static, it is not an inner class - it's a static member class (formerly top-level nested class, but that terminology is wrong now).
[PdH]: Unfortunately, they did not use this definition consistently
It's true they did use it inconsistently, but I think they're much better now. And the JLS is the official language definition after all; texts which contradict this are just plain wrong. Note that the Sun tutorial uses the JLS definition as well. (Aside from ignoring the issue of inner classes in static contexts, which I also ignored above for simplicity.)
[DL]: I think people can use private or protected modifiers instead of inner class, but that will be a lot of confusion.
Starting with me. What are you talking about? The private and protected modifiers can be used as part of inner (or nested) classes; why or how would you use them "instead of"?
For the original question: nested and inner classes are basically ways to create short, simple, convenient class definitions for classes which are closely linked to a particular class, and do not really need to be used in any other context. Among other benefits, this allows us to make details of the class private even more completely than we could otherwise using separate classes defined in different files (which would require at least package (default) access to each other in order to communicate).
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. Sorry to butt into this thread.
I have some inner-class problems after adding a panel p2 inside another panel.
I posted that in the forum "SWING/JFC/AWT" under the thread "cannot add actionListerner..."
Although I can compile successfully after modifying the code from

It now throws a cryptic "NullPointerException" :


This is meaningless to me !
To think that one can cause so much mayhem and damages by simply adding an inner panel ...
Can anyone give me some hints, tips, help etc ?
TIA
[ December 25, 2002: Message edited by: achana chan ]
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vijaya:
What is the need of innerclass?

My answer is clarity.

I know that for protection from outsiders.We can do this by using private or protected modifiers.
My comment is:
I think people can use private or protected modifiers instead of inner class, but that will be a lot of confusion.

My answer or comment could be wrong, but I think I follow the question.
[ December 25, 2002: Message edited by: Don Liu ]
 
Arvind Varma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*************************************
Hi!
What is the need of innerclass?I know that for protection from outsiders.We can do this by using private or protected modifiers.
***********************************
The beauty of Inner Class lies in its ability to access all the class members(even the private members) of the parent class which is not possible when you use modifiers.
Public class Ark
{
private int t1;
protected modArk;
class innerArk()
{
System.out.println(t1);
}
}
public class modArk()
{//You'll need a reference to the parent"Ark" here
System.out.println(t1);
}
if u still r hazy abt it....do post ur Q's...
Thanks
 
Marilyn de Queiroz
Sheriff
Posts: 9082
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijaya,

Hi,

Welcome to JavaRanch! Please adjust your display name to meet the JavaRanch Naming Policy.
You can change it here.

Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!