Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes about inner 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 » Java » Java in General
Bookmark "about inner classes" Watch "about inner classes" New topic

about inner classes

Pradeep Kumar
Ranch Hand

Joined: Sep 11, 2007
Posts: 77
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();

SCJP 1.6
Rob Spoor

Joined: Oct 27, 2005
Posts: 19651

Inside the MyOuter class you can just use it as MyInner. Outside of MyOuter you have to add the MyOuter.

How To Ask Questions How To Answer Questions
Pradeep Kumar
Ranch Hand

Joined: Sep 11, 2007
Posts: 77
I agree. Here's the link:
subject: about inner classes
Similar Threads
Inner class
accessing shadowed final values in method local inner class
method-local inner classes
Inner Class question..
inner class instantiation from outside the the outer class