Solved

Inner classes

Posted on 2002-04-14
4
238 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
pairstar challenge 2 41
Requested array size exceeds VM limit 3 44
Java Server Faces parameter pass? 6 18
maven project error 5 17
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now