• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inner class

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]


 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No difference to my knowledge.But obviously the first method is preferable for better clarity .
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
avn,
No I don't think there is any difference.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic