File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Outside code as an Inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Outside code as an Inner class" Watch "Outside code as an Inner class" New topic
Author

Outside code as an Inner class

jorge Garcia
Greenhorn

Joined: Jan 09, 2008
Posts: 16
Ranchers-

I'm working on program using NetBeans for the first time and I'm trying to instantiate the GUI as an inner class of the Main class where the gui code is sitting in a seperate file.

The compiler gives this error - " symbol not found....class:Gui. I wrote up a couple of small test classes to see if I could get it to work and got the same error. Reading the tutorial it seems it's right but I must be missing something. Any help would be great. Thanks.

Here's the code -

public class Outer {

int a = 100;

public static void main (String[] args) {
Outer outer = new Outer();
outer.outerMeth();

Outer.Inner inner = outer.new Inner();
}

public void useInner() {

inner.go();

}

public void outerMeth() {
System.out.println("Outer meth " + a);
}


}

public class Inner {

public void go () {
System.out.println("Inner Go");

}

public void useOuterInstance variable() {
System.out.println("a= "+a);
}
}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You don't seem to have a Gui class in what you have quoted.
You have named your classes inappropriately; the compiler can't tell that Inner is not an inner class of Outer (at least not in what you have posted).
You have got Outer.Inner inner in the main() method; this is a local variable which you are calling in the useInner() method.

It will probably work when you correct that lot
Lukasz Bajzel
Greenhorn

Joined: Dec 03, 2007
Posts: 26
In the code posted above , the Outer class is complete (}) before the Innner class is declared. So there is no way you will get access to the class by calling
Outer.Inner inner = outer.new Inner();

Now, if you delete that } and add one at the bottom, the structure becomes ok but then again there are other problems.

1. useInner() method cannot access the object 'inner' because it is local to the main method (as pointed out by the previous post)
2. useOuterInstance variable() is not a valid method. It has to be either useOuterInstance_variable() or useOuterInstancevariable()

Make these changes and go from there.

Btw, the note below will help you refresh what you might already know
----------
A nested class is another member of the outer class so it has access to all the members of the enclosing class irrespective of the access modifiers (private, protect or public).

* Being a member of the outer class, a nested class can be declared private, public or default.
* Again a nested class can be declared static (called static nested class) or non-static ( called inner class)

A static nested class, like any other static member, is an class level structure for the outer class.

public class Outer{
public static class Inner{
}
}

The static nested class 'Inner' is accessed like "Outer.Inner "
To create an object of inner,
Outer.Inner inner = new Outer.Inner();

Now, with a non-static nested class (inner class), the object creation is little tricky. In this case, we need to create an object of the outer class first and then use the outer object to create an object of the inner class

public class Outer{
public class Inner{
}
}

The syntax would be like
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
--------------

Good luck!

Sincerely,
Your friends at www.javaadvice.com
www.javaadvice.com - The one stop resource for all your Java questions and answers
jorge Garcia
Greenhorn

Joined: Jan 09, 2008
Posts: 16
I think I'm missing something fundamental here and in reading my first post I see it was kind of sloppy -sorry about that. Let see if I can ask what is confusing me a bit clearer. Thanks again for the help.


'Outer.java' sits in its own file seperate from 'Inner.java' class.

I'd like to create an instance of "Inner.java" in "Outer.java" so that it is just like a nested class inside of "Outer.Java" even though it is sitting in a separate file.

//compiler error is: cannot find symbol. class: Inner.

public class Outer {


public static void main (String[] args) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
}

}

//Sit in a seperate file from Outer.java.
//Compiled before trying to compile Outer.java

public class Inner {

public void go () {
System.out.println("Inner Go");

}
}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Difficult to know what you are doing. What you describe is not creating an inner class; that is creating a field. It now ceases to be appropriate to call the classes Outer and Inner.
The way you have written it doesn't make sense.
You would end up with something like this:That will probably work.
[edit]Inserted body in constructor[/edit]
[ January 26, 2008: Message edited by: Campbell Ritchie ]
jorge Garcia
Greenhorn

Joined: Jan 09, 2008
Posts: 16
Campbell, Man, I'm not getting this.

I read this in Java HeadFirst pg 378:

" You can instantiate an inner instance from code running outside the outer class, but you have to use a special syntax. Chance are you'll go through your entire Java Life and never need to make an inner class from outside, but just in case you're interested....

class Foo {
public static void main (String[] args) {
MyOuter outer Obj = new MyOuter();
MyOuter.MyInner inner Obj = outerObj.new MyInner();
}
}

Isn't this saying you can instantiate an outside class inside a class so that the outside class is now just like an inner class?

Thanks again for your help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I don't know a lot about this either, but the code from HFJ tells you how to instantiate the INNER class, not the outer.
Are you sure it's OuterObj.new Innerclass(), and not OuterClass.new Innerclass()?

But in the code you originally submitted, you didn't have an inner class at all. You might have changed it, but you have two top-level classes. See what Lukasz Bajzel has written; that is how you declare an inner class.
Try changing the code so the Inner class is actually inside the outer class, then see what happens.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I think you are right about outerObj.new InnerClass(); I have just tried it.

Please try it yourself.
jorge Garcia
Greenhorn

Joined: Jan 09, 2008
Posts: 16
Ok. got it to work. Whew, took awhile though.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Originally posted by jorge Garcia:
Ok. got it to work. Whew, took awhile though.


Well done
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Outside code as an Inner class