• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

about inner classes

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I was just working on inner classes and want to know how to instantioate an inner class, In the book its given as
MyOuter.MyInner innerObj = new MyOuter().new MyInner();
But the way i instantiated is
MyInner innerObject = new MyOuter().new Inner();

Are the two declarations have the same effect or is there a diiference in them. Plese reply asap.

Below is the code snippet.

public class MyOuter {
private int x=5;
public class MyInner{
public void justPrint() {
System.out.println("The value of i is "+x);
}
}
public static void main(String args[]) {
MyInner object = new MyOuter().new MyInner();
object.justPrint();
}
}
 
Sheriff
Posts: 22650
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inside the MyOuter class you can just use it as MyInner. Outside of MyOuter you have to add the MyOuter.
 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic