aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner classses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner classses" Watch "inner classses" New topic
Author

inner classses

b bajwa
Greenhorn

Joined: Oct 26, 2001
Posts: 12
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

Joined: Jan 09, 2001
Posts: 1492
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

Joined: Nov 03, 2000
Posts: 267
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

Joined: May 08, 2001
Posts: 356
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).]


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
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

Joined: Oct 26, 2001
Posts: 12
Thank you everyone who helped me
The problem solved(Problem was case of the innerMethod)
 
Don't get me started about those stupid light bulbs.
 
subject: inner classses