Outside code as an Inner class

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.

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

Posts: 26

posted 8 years ago

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(); --------------

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.

//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

Posts: 51453

87

posted 8 years ago

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

Posts: 16

posted 8 years ago

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....

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

Posts: 51453

87

posted 8 years ago

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

Posts: 51453

87

posted 8 years ago

I think you are right about outerObj.new InnerClass(); I have just tried it.

Please try it yourself.

jorge Garcia

Greenhorn

Posts: 16

posted 8 years ago

Ok. got it to work. Whew, took awhile though.

Campbell Ritchie

Sheriff

Posts: 51453

87

posted 8 years ago

Originally posted by jorge Garcia: Ok. got it to work. Whew, took awhile though.