Solved

Inner classes

Posted on 2002-04-14
4
243 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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
map related example 6 59
eclipse console opening separately 2 38
How to configure empty element in XML Document parser? 15 45
junit as external jar or library 7 40
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

726 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