• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner classes

 
Tony Gallagher
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying understand inner classes, but I am unable to read the variable y from the outer class. Seeing as the inner class is encapsulated by the outer class should it not be able to access it?

Thanks

 
Henry Wong
author
Marshal
Pie
Posts: 21420
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Gallagher wrote:
I am trying understand inner classes, but I am unable to read the variable y from the outer class. Seeing as the inner class is encapsulated by the outer class should it not be able to access it?


Sure. But shouldn't you use an instance of the inner class to access it? After all, it is part of (instance variable) the inner class right?

Henry
 
Tony Gallagher
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Henry, I have read that I need to create an instance of the outside class before I can create an instance of my inside class. I also read a little about static nested classes, can I declare my nested class and variables as static and access using class.variablename or this this only work from inside the inner class?

I hope this makes sense, i'm completely confused with this topic.

Thanks Again





I also tried it in a test class which seems to make more sense but no joy, still can't see y


 
Henry Wong
author
Marshal
Pie
Posts: 21420
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Gallagher wrote:
I also tried it in a test class which seems to make more sense but no joy, still can't see y



Not sure what you mean by "make more sense". You created an inner class, but didn't actually use it. Try...



Henry
 
Jason Bullers
Ranch Hand
Posts: 111
8
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Henry explained should work just fine.

I just wanted to point out a few things about the inner class you have created. Instances of an inner class declared in the way you have done it is tied to an instance of the outer class, as you already know. That means that for an instance of Inside to exist, there must be an instance of Outside to hold it. That's why you are able to refer to the variable 'x' in Inside.add(): when add() is invoked, you are guaranteed to have a variable 'x' that exists in the outer object (you can actually access it the verbose way as Outer.this.x, which comes in handy sometimes if the inner class has methods or fields with the same name as the outer class).

Also, when you are creating an instance of Inside within the Outside class, you don't need the "fancy" syntax: you can just create it like you would any other object. Probably, you would want to do something like this:



The object of type Inside you create would implicitly be created in the Outside object you invoked Multiply() on. You could do that like this:



In your Multiply() method, the value assigned to 'total' before the println() would be 600, as you would expect.
 
Randall Twede
Ranch Hand
Posts: 4469
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
between Henry and Jason you should have a pretty good answer. like Jason said just create an instance of the inner class the same way you create any variable. inner classes have a "special" advantage, and it is one of the main reasons to use them. they have direct access to the variables of the outer class. it is also a reason to be careful in using them since it becomes very easy to write "bad" code.
 
Amey Ambulgekar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Gallagher wrote:Thanks for the reply Henry, I have read that I need to create an instance of the outside class before I can create an instance of my inside class. I also read a little about static nested classes, can I declare my nested class and variables as static and access using class.variablename or this this only work from inside the inner class?

I hope this makes sense, i'm completely confused with this topic.

Thanks Again





I also tried it in a test class which seems to make more sense but no joy, still can't see y




hello Tony Gallagher,

From Henry's explanation you got an idea about inner-outer class and one point i wanted to tell about this is that in your program in Multiply() method without "Outside.Inside inner = outer.new Inside(30);" statement you can directly create object of inner class and access method of its.. yes, tried it... you may try and you will get clear cut idea of it..

Thanking you.
 
Tony Gallagher
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the great reply's guys and Happy New Year, I tested this from Jason's code example and it does work.
I'm still confused about one thing though, how come when creating the inner class you don't have to use the "weird" syntax that references the outer object? and when would one use this?

Thanks again
 
Jason Bullers
Ranch Hand
Posts: 111
8
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Gallagher wrote:I'm still confused about one thing though, how come when creating the inner class you don't have to use the "weird" syntax that references the outer object? and when would one use this?


Let's start with the left side: the type. When do you need to declare the type as Outside.Inside, and when can you just use Inside?

You don't have to use the fully qualified name when working with the inner class inside the outer class. It's similar to how you can access static fields of a class without having to use the class name to qualify it. Within the outer class, the name of the inner class is known and so you can use it directly. However, if you are creating an instance of a public inner class from elsewhere in the program, then you have to use the fully qualified name. So, for example, when you wrote your TestNested class, in its main() method, you would need to use the longer syntax to qualify it properly.

Now, on to the right side: object creation. When do you need to use .new, and when can you just use new?

This lines up pretty much with the rules above: if you are executing a method of the outer class, then you already know what instance of Outside will have an instance of Inside created for it. However, if you are creating an instance of the inner class from elsewhere in the program, you have to have an instance of the outer class to associate it with. By writing outer.new Inside(30), you are telling java that a new inner class instance should be created and connected to the object referenced by 'outer'.

Note that if you don't need that reference to the outer class, you should define your inner class as static. You never need the weird syntax when creating instances of a static nested class.
 
Tony Gallagher
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jason, great explanation. That makes things so much clearer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic