• Post Reply Bookmark Topic Watch Topic
  • New Topic

static inner class.  RSS feed

 
ashok khetan
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
one query---Is it illegal to associate a static class reference with a reference of its enclosing class?( e.g. in the following code)
code:
_____________
public class top {
static class inner {
void show() {
System.out.println("show() in inner");
}
}
public static void main(String []args) {
inner i = new top().new inner();
i.show();
}
}
_____________
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you donot need to instantiate the outer class.
The static class can be invoked directly. For eg, if
Inner class is an static (top-level nested class ) and Outer
is a outer class then we just need to do the following :

Outer.Inner inner = new Outer.Inner();
 
vipin bhatia
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it is just like reffering to any static member of a class, for example in
class Temp
{
static i;
}
Here i can be reffered as Temp.i or temp.i where Temp temp = new Temp.

Originally posted by ashok khetan:
hi,
one query---Is it illegal to associate a static class reference with a reference of its enclosing class?( e.g. in the following code)
code:
_____________
public class top {
static class inner {
void show() {
System.out.println("show() in inner");
}
}
public static void main(String []args) {
inner i = new top().new inner();
i.show();
}
}
_____________

 
anurag dwivedi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashok,
It is not illegal, though not required to instantiate an outer class to access a static inner class.
I guess im not wrong.
Anurag
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anurag is right...



Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 12, 2001).]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!