• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why This compiler error?

 
Gary Farms
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following program contains a member inner class called Inner which contains a method called fn. I call it from main in the enclosing class PixelOps.
When I compile it, I get a compile error: stating that a '(' is expected on the following line: PixelOps.Inner mc = ec.new PixelOps.Inner(); Why do I get this? Also, does a member inner class always have to be declared public?
Here's the short program:
public class PixelOps
{
static int result;
public static void main(String[] args)
{
int dividend = 100;
int divisor;
divisor = 0;
PixelOps ec = new PixelOps();
PixelOps.Inner mc = ec.new PixelOps.Inner();
mc.fn(dividend, divisor);
}
public class Inner
{
public void fn(int top, int bottom)
{
try
{
result = top / bottom;
System.out.println("result = " + result);
}
finally
{
System.out.println("finally entered");
}
}
}
}
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change
PixelOps.Inner mc = ec.new PixelOps.Inner();
to
PixelOps.Inner mc = ec.new Inner();
You already created a PixelOps (ec) so the new there wasn't needed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic