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


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner Class Question...." Watch "Inner Class Question...." New topic
Author

Inner Class Question....

Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
Hi,
Can anybody pls clarify my doubt.Following is the material from javarach Campfile about Inner classes.
"A static nested class can be instantiated, but the object created doesn't share any special relationship with an outer object.
The static nested class is tied only to the outer class, not an instance of the outer class.
Outer.Inner i = new Outer.Inner();
That's why you can make an instance of the static nested class without having an instance of the outer class, just the way you can call static methods of a class without having any instances of that class. A top-level nested class is little more than another way to control namespace. "
But my doubt is why can't we refer inner class directly like this
Outer.Inner i = Outer.Inner(); //note without new keyword.
As the inner class is static, it will't require to an object of itself.
Regards
Prasad

Udayan Naik
Ranch Hand

Joined: Oct 18, 2000
Posts: 135
A static inner class is like a top level class.It can contain data members and methods in itself.Now these data members and methods may or may not be static.So now my question to u is , HOW WILL U ACCESS THESE VARIABLES WITHOUT AN OBJECT OF THE STATIC INNER CLASS?
------------------
Come on in !! Drinks are on the house in the Big Moose Saloon !!


Udayan Naik<BR>Sun Certified Programmer for the Java 2 Platform
Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
udaya,
I will clear ur doubt.if you are accessing the non-static data members then the object is required.Assume if ur accessing static member, Then i think we don't require the object.

Actually my question was..Can we refer to the inner class like this.here getValue() is also a static menthod(assume that it returns an Outer object).

Outer.Inner i = Outer.Inner().getValue(); //note without new keyword.
Prasad

Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3324
    
    8
It would really help you if you actually compile a small test program.
Anyway,
You said: Outer.Inner i = Outer.Inner().getValue(); //note without new keyword.
Now, do you really think Outer.Inner() is valid? Assume that the outer class also has static method named Inner.
What would Outer.Inner() mean then?
-Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
Paul.
If u don't mind.I would like share my little knowledge ,as we know if the data member is static then we can directly refer that data member by classname.methodname ex: Math class.
Can we apply the same concept here for inner classes.
Pls help me in concept.
Prasad



[This message has been edited by Prasad Ballari (edited October 24, 2000).]
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3324
    
    8
Yes we can.
Consider this:
public class A
{
static Object obj = new Object();
}
Now here, you can use obj without instantiating A ( A.obj) right.
But here, the Object obj is actually there. It is already instanciated.
Now, in case of static inner class:

You can refer i (as it is static) with out having the objects for either Outer or Inner. But to access k, you need an object of class Inner. You can do that by doing new Outer.Inner()
new Outer.Inner() does not mean that you are creating an Object of Outer also. You are only creating an object of class Inner.
The syntax that you wrote: Outer.Inner() //without new, is a syntax of using static method Inner() of class Outer. But as you know, Inner is not a method but a class. So it will fail.
HTH,
Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
a hui
Ranch Hand

Joined: Oct 14, 2000
Posts: 39
Hi,
I read these message and I understand how to user inner classes.
My question is when do you want to use inner class? Could you provide any examples?
Thanks in advance
ah
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner Class Question....