Solved

Inner classes

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thymeleaf natural templating vs JSP 2 103
xampp tool 12 50
difference of if loops 23 49
Desingning Refactoring existing code 2 24
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

809 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