473,322 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Inner classes in Java

184 100+
Hi,
please any one say me..
why we go for inner classes in Java??
And importance behind that??
please do needfull

-Thanks,
Hamsa
Sep 10 '07 #1
11 1889
r035198x
13,262 8TB
Hi,
please any one say me..
why we go for inner classes in Java??
And importance behind that??
please do needfull

-Thanks,
Hamsa
And what does your Java textbook say?
If it doesn't have a section for that, please throw it away.

Here are some links to some good tutorials. Download Sun's tutorial. Its got a section for all that.
Sep 10 '07 #2
Nepomuk
3,112 Expert 2GB
Hi,
please any one say me..
why we go for inner classes in Java??
And importance behind that??
please do needfull

-Thanks,
Hamsa
The importance of inner classes is, that they make life easier and safer. You could have separate classes in separate files for everything, but often you just want to use some class in one other class and that's when you use it. It can even be a private class then, as only the other classes in that file will have to see it. (Which makes it less vulnerable.)

Greetings,
Nepomuk
Sep 10 '07 #3
r035198x
13,262 8TB
.. You could have separate classes in separate files for everything, but often you just want to use some class in one other class and that's when you use it. ...
Remember that you are usually using some class in some other class even without having inner classes.
Sep 10 '07 #4
Nepomuk
3,112 Expert 2GB
Remember that you are usually using some class in some other class even without having inner classes.
OK, what I meant was:
"You could have separate classes in separate files for everything, but often you just want to use some class in only one other class and that's when you use it."

Greetings,
Nepomuk
Sep 10 '07 #5
r035198x
13,262 8TB
OK, what I meant was:
"You could have separate classes in separate files for everything, but often you just want to use some class in only one other class and that's when you use it."

Greetings,
Nepomuk
<Nitpicking again>
Inner classes can be instantiated from other classes that are not their outer classes anyway.
</Nitpicking again>
Sep 10 '07 #6
JosAH
11,448 Expert 8TB
Maybe the following example clarifies matters a bit:

Expand|Select|Wrap|Line Numbers
  1. public class Star {
  2.  
  3.     private String name;
  4.  
  5.     public Star(String name) { this.name= name; }
  6.  
  7.     public class Planet {
  8.  
  9.         private String name;
  10.  
  11.         public Planet(String name) { this.name= name; }
  12.  
  13.         public class Moon {
  14.  
  15.             private String name; 
  16.  
  17.             public Moon(String name) { this.name= name; }
  18.  
  19.             public String toString() { return name+" (orbiting "+Planet.this+")"; }
  20.         }
  21.  
  22.         public String toString() { return name+" (orbiting "+Star.this+")"; }
  23.     }
  24.  
  25.     public String toString() { return name; }
  26.  
  27.     public static void main(String[] args) {
  28.  
  29.         System.out.println(new Star("sun").new Planet("earth").new Moon("moon"));
  30.     }
  31. }
  32.  
kind regards,

Jos
Sep 10 '07 #7
r035198x
13,262 8TB
Maybe the following example clarifies matters a bit:

Expand|Select|Wrap|Line Numbers
  1. public class Star {
  2.  
  3.     private String name;
  4.  
  5.     public Star(String name) { this.name= name; }
  6.  
  7.     public class Planet {
  8.  
  9.         private String name;
  10.  
  11.         public Planet(String name) { this.name= name; }
  12.  
  13.         public class Moon {
  14.  
  15.             private String name; 
  16.  
  17.             public Moon(String name) { this.name= name; }
  18.  
  19.             public String toString() { return name+" (orbiting "+Planet.this+")"; }
  20.         }
  21.  
  22.         public String toString() { return name+" (orbiting "+Star.this+")"; }
  23.     }
  24.  
  25.     public String toString() { return name; }
  26.  
  27.     public static void main(String[] args) {
  28.  
  29.         System.out.println(new Star("sun").new Planet("earth").new Moon("moon"));
  30.     }
  31. }
  32.  
kind regards,

Jos
Thankfully not A T F E.
Sep 10 '07 #8
kreagan
153 100+
<Nitpicking again>
Inner classes can be instantiated from other classes that are not their outer classes anyway.
</Nitpicking again>
How? Even if the class is public, I don't think you can access that class. You can, however, access the class indirectly. (I don't have access to Java Dev. Env. to check, however).

Expand|Select|Wrap|Line Numbers
  1. public Class1{
  2.         public InnerClass temp;
  3.  
  4.         public Class1(){  temp = new InnerClass(); }
  5.         public InnerClass(){
  6.                public void someMethod(){}
  7.         } 
  8. }
Expand|Select|Wrap|Line Numbers
  1. public Class2{
  2.        public void someMethodInClass2(){
  3.              InnerClass temp = new InnerClass();   //I'm sure this will give compiler error.
  4.              Class1 temp = new Class1();
  5.              temp.temp.someMethod(); //this should work. This is how you access an inner class from outside that class. 
  6.        }
  7. }
I will bet money that the line " InnerClass temp = new InnerClass();" in Class2 will not work.

So how do you instantiate an object from an inner class?
Sep 10 '07 #9
r035198x
13,262 8TB
How? Even if the class is public, I don't think you can access that class. You can, however, access the class indirectly. (I don't have access to Java Dev. Env. to check, however).

Expand|Select|Wrap|Line Numbers
  1. public Class1{
  2.         public InnerClass temp;
  3.  
  4.         public Class1(){  temp = new InnerClass(); }
  5.         public InnerClass(){
  6.                public void someMethod(){}
  7.         } 
  8. }
Expand|Select|Wrap|Line Numbers
  1. public Class2{
  2.        public void someMethodInClass2(){
  3.              InnerClass temp = new InnerClass();   //I'm sure this will give compiler error.
  4.              Class1 temp = new Class1();
  5.              temp.temp.someMethod(); //this should work. This is how you access an inner class from outside that class. 
  6.        }
  7. }
I will bet money that the line " InnerClass temp = new InnerClass();" in Class2 will not work.

So how do you instantiate an object from an inner class?
Have you read Jos' example above?
Sep 10 '07 #10
kreagan
153 100+
Have you read Jos' example above?
Lol... nope. Just did though.
Sep 10 '07 #11
JosAH
11,448 Expert 8TB
How? Even if the class is public, I don't think you can access that class. You can, however, access the class indirectly. (I don't have access to Java Dev. Env. to check, however).

Expand|Select|Wrap|Line Numbers
  1. public Class1{
  2.         public InnerClass temp;
  3.  
  4.         public Class1(){  temp = new InnerClass(); }
  5.         public InnerClass(){
  6.                public void someMethod(){}
  7.         } 
  8. }
Expand|Select|Wrap|Line Numbers
  1. public Class2{
  2.        public void someMethodInClass2(){
  3.              InnerClass temp = new InnerClass();   //I'm sure this will give compiler error.
  4.              Class1 temp = new Class1();
  5.              temp.temp.someMethod(); //this should work. This is how you access an inner class from outside that class. 
  6.        }
  7. }
I will bet money that the line " InnerClass temp = new InnerClass();" in Class2 will not work.

So how do you instantiate an object from an inner class?
By having a reference available of the outer class; that's all there is to it. Read
my Star/Planet/Moon example again. If there is a star in scope you can create
a planet for it, if there is a planet in scope you can create a moon for it etc. etc.

kind regards,

Jos
Sep 10 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Average_Joe | last post by:
Hello PHP people, Was wondering if PHP5 had some sort of "nested class" functionality. I'm trying to figure out how to return an object (with a private constructor) that has access to...
2
by: Murat Tasan | last post by:
i have an inner class and an outer class, both of which have an identically named member. how from the inner class can i reference the outer member? (the example code should illustrate this...
5
by: devu | last post by:
Currently I'm working on a class that performs a batch process as per scheduling for all employees in a company. Being a batch process load is obviously a huge factor. The process first needs to...
10
by: Paul Morrow | last post by:
I'm hoping that someone can explain why I get the following exception. When I execute the code... ###################################### class Parent(object): class Foo(object): baz = 'hello...
6
by: Marco | last post by:
Howdy! Given: public abstract class A { public abstract int A1(int i); private class B { private int B1(int i) { int j;
9
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order */) { /* Check if the product order can be added...
6
by: Philip Potter | last post by:
I have a graph data structure. It goes something like this: class GraphNode { private: friend class Graph; // successor GraphNodes stored as indices of the Graph's array int value; int next1,...
9
by: Matthias Buelow | last post by:
Hi folks, I've got something like: class Outer { int f(); friend class Inner; class Inner { int g() {
3
by: ChrisW | last post by:
Hiya, So I have a class that creates threads within it. These threads are a class underneath the parent class. I want to access values in the parent class from the threads while they run. Yet...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.