wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes nested classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "nested classes" Watch "nested classes" New topic
Author

nested classes

Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
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

Joined: Nov 12, 2002
Posts: 40
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.


ciao<br />Igor Zeta<br />SCJP1.4
Rahul Gupta
Ranch Hand

Joined: Nov 20, 2002
Posts: 37
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


regards,<p>rahul <p>SCJP 1.4<br />--------<br />Life is great, make it meaningful<br />---------------------------------
Igor Zeta
Ranch Hand

Joined: Nov 12, 2002
Posts: 40
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

Joined: Nov 07, 2002
Posts: 298
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

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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 ]

"I'm not back." - Bill Harding, Twister
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Thanks Jim, I misunderstood that part of JLS.
Igor Zeta
Ranch Hand

Joined: Nov 12, 2002
Posts: 40
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

Joined: Jul 03, 2001
Posts: 2120
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.
 
Consider Paul's rocket mass heater.
 
subject: nested classes
 
Similar Threads
class extension error
Using Other class
please answer the question and explain it to me,thanks!
What are private classes?
public class filename????