Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner classses

 
b bajwa
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How i can invoke inner class method. the code are the following
Code:
public class OuterOne
{
static private int x;
static public class InnerOne
{
private int y;
public void innerMethod()
{
int z=6;
System.out.println("enclosing z is "+z);
System.out.println("enclosing x is "+x);
System.out.println("y is "+y);

}
}
public void OuterMethod()
{
System.out.println("x is "+x);
}
public void makeInner()
{
InnerOne anInner= new InnerOne();
anInner.innerMethod();
}
public static void main(String args[])
{
OuterOne o=new OuterOne();
InnerOne i= new InnerOne();
//how i can invoke InnerMethod of the static
// inner class
i.InnerMethod(); // not working
o.InnerMethod(); //Not working
}
}
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi b,
You are having case sensitivity problems. The first choice would work if you had the case of I correct:
Method created: innerMethod
Method called: InnerMethod
See a problem here. Change your first "not working" call to:
i.innerMethod();
and all is well.
Regards,
Manfred.
 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are instantiating the outer class by saying new OuterOne() & you are trying to call the inner class method from the outerclass object & thats why you are are not able to get it working..Here is what you have to do:
OuterOne.InnerOne inn = new OuterOne.newInnerOne(); //Inner class
inn.innerMethod(); // call inner class method
Remember that you always have to create an object of the outer class before you create an object of the inner class.
Let me know if you need more info...
Hope my explanation helped
Originally posted by b bajwa:
How i can invoke inner class method. the code are the following
Code:
public class OuterOne
{
static private int x;
static public class InnerOne
{
private int y;
public void innerMethod()
{
int z=6;
System.out.println("enclosing z is "+z);
System.out.println("enclosing x is "+x);
System.out.println("y is "+y);

}
}
public void OuterMethod()
{
System.out.println("x is "+x);
}
public void makeInner()
{
InnerOne anInner= new InnerOne();
anInner.innerMethod();
}
public static void main(String args[])
{
OuterOne o=new OuterOne();
InnerOne i= new InnerOne();
//how i can invoke InnerMethod of the static
// inner class
i.InnerMethod(); // not working
o.InnerMethod(); //Not working
}
}

 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inner class is static, so you dont need an instance of the outer class, all you need is a instance of inner class i.e
new InnerOne().innerMethod();
OR
InnerOne i= new InnerOne();
i.innerMethod();
OR
OuterOne.InnerOne io = new OuterOne.InnerOne();
io.innerMethod();
--Farooq
[This message has been edited by Muhammad Farooq (edited October 26, 2001).]
 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops !!! I did not see that you are right..
Originally posted by Muhammad Farooq:
Inner class is static, so you dont need an instance of the outer class, all you need is a instance of inner class i.e
new InnerOne().innerMethod();
OR
InnerOne i= new InnerOne();
i.innerMethod();
OR
OuterOne.InnerOne io = new OuterOne.InnerOne();
io.innerMethod();
--Farooq
[This message has been edited by Muhammad Farooq (edited October 26, 2001).]

 
b bajwa
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone who helped me
The problem solved(Problem was case of the innerMethod)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic