Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

nested classes

 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference betwen A and B???
what is the scope of A and B???
where can I create instances of these classes???
public class Super{

public class A{
}
public static class B{
}

}
 
Igor Zeta
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try to give an answer...
1)The difference between A and B is that the 1st need to be instantiated while the 2nd doesn't.
Again The static one can't access non static properties and methods of the enclosed class while the other is able to access either the types.May be more...
2)The scope is public,isn't it?
3)Where?Wherever you want.
AlThough the static nested one doesn't need to be instatied.
Please Correct me if I'm wrong.
 
Rahul Gupta
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Igor...I dont agree with your explanation that A can be instantiated and B need not ....
Actually A being a non-static inner class can be instantiated only when an outter reference i.e. Super exists..for example
Super s = new Super();
Super.A a = s.new A();
However we can instantiate B, a static inner class, without having a reference of Super.. for example
Super.B b = new Super.B();
Regarding scope you are correct that we can instantiate anywhere since it is public.
rahul
 
Igor Zeta
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Gupta:

Actually A being a non-static inner class can be instantiated only when an outter reference i.e. Super exists..for example
Super s = new Super();
Super.A a = s.new A();

Why you say that has to exists an outter reference
if I can write Super.A a = new Super.A()?;
so in the same way i can do with B in the enclosing class.
After all in both cases have to be an instance.
So Apologizing for the little confusion I've done
between static reference and static class declaration...
I've arrived at this code:

And All is clear...
But What about If I create another class Super2 in the same package?

Why I get this compilation error?

"Super2.java": Error #: 478 : enclosing class Super of class Super.A is not in scope at line 15, column 30

PS:
I'm very interesested in the nested class subject because of a lacking.
I have to understand before taking the exam!!!
Help me do clearness...
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you!!!
guess I'm understanding.... Every thing is OK until the part of the instantiation!!! but what is the scope of a public nested class if the owner of this nested class has package level access??? like the following:
class Owner{
public class Nested1{
}
public static class Nested2{
}
}

just one more question...
Some people say that not all nested classes are inner classes!!! Where can I find information about this topic???
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A non-static nested class (NSN from now) cannot exist without a relationship with an instance of the containing class.
Why the following is compiling?
void TryMetodh(){
Super.A a = new Super.A();
}
because the hidden "this" reference is associated to the instance of A.
Within the body of a constructor "this" points to the object being created. Within the body of an instance method "this" points to the instance on which the method was called. The compiler places "this" as the first argument to the A constructor. This paremeter within the body of the constructor is assigned to a variable created automatically by the compiler in the NSN class. In this way the instance of the NSN class is associated to an instance of its containing class. Through this reference the NSN object can access all the members of the instance of the containig class.
It is not possible to create a NSN class instance without a reference for the containing class:
public class Super2 {
void TryMetodh(){
Super.A a=new Super.A();
}
}//compiler error
Here "this" points to an instance of Super2.
Running javap -c MyClass is possible to see the hidden parameter to the constructor and the automatically created field in the NSN class.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[JB]: A non-static nested class (NSN from now) cannot exist without a relationship with an instance of the containing class.
We call those NSNs "inner classes". Henceforth anyone using the term "inner" to refer to a static nested class shall be shot. There is no such thing as a static inner class; "inner" implies "non-static".
Inner classes do not actually require instances of the enclosing class in some cases. See my post here for details.
[lo]: Some people say that not all nested classes are inner classes!!! Where can I find information about this topic???
They're right. All nested classes are either inner classes, or static member classes (formerly known as top-level nested classes) - one or the other, not both. Some books or people may contradict this statement, but they are wrong. See the JLS for details.
It's possible you may see these terms misused in sample exams or even on the real exam. Usually you can figure out what they mean from context (especially on the real exam, where they will probably give you sample code and ask you how it behaves, without worrying about how you define something as "inner" or "top-level"). Mock exams are often completely wrong on these issues however. Just make your best guess what the intent of a question is, and don't worry about it too much afterwards.
[ January 24, 2003: Message edited by: Jim Yingst ]
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim, I misunderstood that part of JLS.
 
Igor Zeta
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So outside the NSN class's owner it's impossible to have a call to a method or a reference to a member of the NSN class?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need a reference to an instance of the outer class, or just a reference to the proper inner class if it is of the type Jim explained.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic