Solved

Inner classes

Posted on 2002-04-14
4
245 Views
Last Modified: 2010-03-31
I am getting "<identifier> expected" and "cannot resolve symbol" messages from the compiler on line 18 (System.out.println) below. The code is meant to allow me to test some of the rules of inner classes inside of a method. Thanks for the help.

class Outer {

int x, y, z;


public Outer (int a, int b)  {
  x = a;
  y = b;
}

void prod () {

  z = x*y;

  class Inner {    
     System.out.println("x = " + z);
  }

}

}
0
Comment
Question by:amcandrew
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Expert Comment

by:tsachi
ID: 6939887
the inner class should be outside of the method prod().
it's a new class inner to this class but it's not related to the method in any way.
you can still use it in the method but in a different way
void prod()
{
z=x*y;
Inner i = new Inner();
i.print();
}

class Inner{
  void print()
  {
     System.out.println("x="+z);
  }
}
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 50 total points
ID: 6939888
You should define some methods of the inner class rather than a statement like "System.out.println("x="+z)". You may encapsulate this statement in some method.

Like this example:

class Outer {

int x, y, z;


public Outer (int a, int b)  {
 x = a;
 y = b;
}

void prod () {

 z = x*y;

 class Inner {    
    public void method1() {
    System.out.println("x = " + z);
    }
 }

 Inner i = new Inner();
 i.method1();

}

public static void main(String[] args) {
   Outer x = new Outer(30, 10);

   x.prod();
}
}
0
 

Author Comment

by:amcandrew
ID: 6941252
Thanks to Taschi for his answer.

 I rejected it because the purpose of my code was to test inner classes inside of a method and the solution he proved, which worked, involved a member inner class, i.e. positioned directly below the enclosing class. The second answer provided by pkwan better addressed my objective.

Alec McAndrew

0
 

Author Comment

by:amcandrew
ID: 6941283
Thanks for the answer.

I do have one additional question, though. The code you suggested compiles fine, which surprised me a bit. I understood from books I am reading that references to variables made inside of "local" inner classes, i.e. inner classes that are inside of a method, need to be references to final variables. In this case, method1() refers to z, which is an instance variable of Outer and is not final. Why doesn't this cause a problem?
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question