wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes creating an inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating an inner class" Watch "creating an inner class" New topic
Author

creating an inner class

chris czinder
Greenhorn

Joined: Oct 02, 2002
Posts: 28
Ok,
I know I'm going to feel dumb when I get an answer to this, but here it goes.
I need to create an inner class and have it encapsulate stuff.
Well uhm....how do you create an inner class. I've looked at documentation here and on sun.java. but I still don't get it.
Any help would be greatly appreciated.
Thanks
chris
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Why do you need to create an inner class?
Normal non-inner classes can also "encapsulate stuff".


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
chris czinder
Greenhorn

Joined: Oct 02, 2002
Posts: 28
I need to use an inner class to create get and set methods for 3 instance variables. The inner class will also have a constructor to insantiate an object using the received values of the 3 variables
Chris
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
It may work well to discuss the details of some example code - perhaps some simple code that you are trying to figure out...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

I am not saying you don't need an inner class but

I need to use an inner class to create get and set methods for 3 instance variables.

this is not a good reason. The get and set methods you create in an inner class will only be accessable by the outer class. No other class will be able to use them, even if they are declared 'public'.
But here is an example:

Other people can jump in here and explain why this code doesn't really do anything for you.


Please ignore post, I have no idea what I am talking about.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
The get and set methods you create in an inner class will only be accessable by the outer class. No other class will be able to use them, even if they are declared 'public'.
That's not really true. Were the inner class private, then it and its members would not be accessible from outside of the enclosing (outer) class.
[ October 24, 2002: Message edited by: Dirk Schreckmann ]
chris czinder
Greenhorn

Joined: Oct 02, 2002
Posts: 28
Ok, Well I only need the Outter class to get the information from the inner class, so that is not a concern. The app i'm writing only has the one class. As far as some code this is what i've got going so far with it.

I know there is more to set and get then that, and i know i need to declare variables, but i understand that. I'm just curious, if i'm setting up the outter inner class thing right.
thanks
Chris
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

You don't need to write the whole thing to compile it. Just give it a shot.
The only I can see is that you have "Class" instead of "class".
chris czinder
Greenhorn

Joined: Oct 02, 2002
Posts: 28
Thanks everyone for their input, additional input is always welcome, but I think I've got it figured out. I'll let you all know later

Chris
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: creating an inner class