wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner class 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 "inner class" Watch "inner class" New topic
Author

inner class

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
Is there any difference if an inner class is created by
1)Outerclassname.innerclassname a1 = new Outerclassname().new innerclassname();
2)innerclassname a2 = new Outerclassname().new innerclassname();
Also, why do i get error if i try to access
a method of outer class using inner class
instance?
Thanks!

[This message has been edited by avn (edited July 10, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by avn:
Hi,
Is there any difference if an inner class is created by
1)Outerclassname.innerclassname a1 = new Outerclassname().new innerclassname();
2)innerclassname a2 = new Outerclassname().new innerclassname();
Also, why do i get error if i try to access
a method of outer class using inner class
instance? like innerclassobject.outerclasmethod();
I also have a doubt with the foll. program
class CJout1
{
private int s1;
private String a="Hi";
void outm1()
{
//7 CJinner1 p2 = new CJinner1();
//8 System.out.println("outer"+p2.i1+p2.i2+s1+a);
}
class CJinner1
{ String i1="inner";
private String i2="private inner";
}
public static void main(String a[])
{
CJout1 c1 = new CJout1();
//17 CJout1.CJinner1 c2 = c1.new CJinner1();


}
}
My doubt is why is that the innerclass objects are created
in a different manner at line7 and at line 17 even though
botn are being accessed from methods of the same outer class.
Please anyone clarify my doubt.I do want to be clear about
innerclasses.
Thanks!

[This message has been edited by avn (edited July 10, 2000).]

Jayakumar Mundanat
Greenhorn

Joined: Jun 17, 2000
Posts: 11
The instance methods of outer class requires an object of the outer class.(not of the inner class!!!).
Regarding the second part of your question,the main() of CJout1 is a static method whereas outm1() is instance method.So whenever you call outm1() you are already within the scope of a outer class(CJout1) instance ,whereas in static main an expilicit instantiation of CJout1 is required before an inner class instance (CJinner1) has to be created,Right??
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks! for the reply jayakumar.But can u also tell me that,
Is there any difference if an inner class is created by
1)Outerclassname.innerclassname a1 = new Outerclassname().new innerclassname();
2)innerclassname a2 = new Outerclassname().new innerclassname();
[This message has been edited by avn (edited July 10, 2000).]
Jayakumar Mundanat
Greenhorn

Joined: Jun 17, 2000
Posts: 11
No difference to my knowledge.But obviously the first method is preferable for better clarity .
Sandra Marti
Ranch Hand

Joined: Jun 08, 2000
Posts: 63
avn,
When you say, Outerclassname.innerclassname a1 = new Outerclassname().new innerclassname();, you have created an object of the nested class type that is associated with the object of the outerclassname. We are creating an object of type innerclassname in the context of the object Outerclassname.
However, within non-static methods that are members of Outerclassname, you can use the class name innerclassname without any qualification as it will be automatically qualified by the compiler with the this variable. So you could create new innerclassname object from within the method of the object Outerclassname:
innerclassname a2 = new innerclassname();
which is equivalent to:
this.innerclassname a2 = this.new innerclassname();
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Thanks for the reply to both of u.But Snadra my doubt was would there be any difference when innerclass is created in main method using
1)Outerclassname.innerclassname a1 = new Outerclassname().new innerclassname();
2)innerclassname a2 = new Outerclassname().new innerclassname();
ThankYou
Sandra Marti
Ranch Hand

Joined: Jun 08, 2000
Posts: 63
avn,
No I don't think there is any difference.
eskay kumar
Ranch Hand

Joined: Jul 22, 2000
Posts: 71
1)Outerclassname.innerclassname a1 = new Outerclassname().new innerclassname();
2)innerclassname a2 = new Outerclassname().new innerclassname();
Yes there is difference in the above declarations.
1 is when u are creating the instance of inner class in another class(not the outer class) and a reference is made to the name of the inner class via the outer class name, like we refer to a class(say trial1) in a package(say pack1)
as pack1.trial1 Similarly we refer to the inner class as outerclassname.innerclassname
2 is when u r making the instance of the inner class in the outer class itself, so obviously no such reference has to be made
eskay
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inner class
 
Similar Threads
Tricky Class Name..?
outer class from inner class
static inner classes
Inner class
Empty Private Constructor Class: Why?