Inner classes

This is a discussion on Inner classes within the Java forums in Programming Languages category; Can someone please guide me a link on java inner classes ?. I am having a tough time getting the nuances of the access mechanism for outer and inner class variables. thanks, milon...

Go Back   Application Development Forum > Programming Languages > Java

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-27-2008, 11:00 AM
katyusha
Guest
 
Default Inner classes




Can someone please guide me a link on java inner classes ?. I am
having a tough time getting the nuances of the access mechanism for
outer and inner class variables.




thanks,
milon
Reply With Quote
  #2  
Old 08-27-2008, 11:34 AM
bugbear
Guest
 
Default Re: Inner classes

katyusha wrote:
>
>
> Can someone please guide me a link on java inner classes ?. I am
> having a tough time getting the nuances of the access mechanism for
> outer and inner class variables.
>


http://java.sun.com/docs/books/tutor...erclasses.html

BugBear
Reply With Quote
  #3  
Old 08-27-2008, 11:59 AM
Roland de Ruiter
Guest
 
Default Re: Inner classes

On 27-8-2008 17:34, bugbear wrote:
> katyusha wrote:
>>
>>
>> Can someone please guide me a link on java inner classes ?. I am
>> having a tough time getting the nuances of the access mechanism for
>> outer and inner class variables.
>>

>
> http://java.sun.com/docs/books/tutor...erclasses.html
>
> BugBear


Probably OP should start reading one page earlier to get a completer
picture:
<http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html>. This
page refers to
<http://blogs.sun.com/darcy/entry/nested_inner_member_and_top>, which
gives an even more detailed description of the relations of the various
types of classes.
--
Regards,

Roland
Reply With Quote
  #4  
Old 08-27-2008, 12:06 PM
bugbear
Guest
 
Default Re: Inner classes

Roland de Ruiter wrote:
> On 27-8-2008 17:34, bugbear wrote:
>> katyusha wrote:
>>>
>>>
>>> Can someone please guide me a link on java inner classes ?. I am
>>> having a tough time getting the nuances of the access mechanism for
>>> outer and inner class variables.
>>>

>>
>> http://java.sun.com/docs/books/tutor...erclasses.html
>>
>> BugBear

>
> Probably OP should start reading one page earlier to get a completer
> picture:
> <http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html>. This
> page refers to
> <http://blogs.sun.com/darcy/entry/nested_inner_member_and_top>, which
> gives an even more detailed description of the relations of the various
> types of classes.


Yeah - most likely - I just hit "java inner classes tutorial"
in google, and cut 'n' pasted the first hit, which appears to be more
than the OP did.

BugBear
Reply With Quote
  #5  
Old 08-27-2008, 12:53 PM
RedGrittyBrick
Guest
 
Default Re: Inner classes


katyusha wrote:
>
>
> Can someone please guide me a link on java inner classes ?.


https://java.sun.com/docs/books/tuto...OO/nested.html


> I am
> having a tough time getting the nuances of the access mechanism for
> outer and inner class variables.
>


Maybe you could compose some example code that shows the aspects you are
having problems with?


--
RGB
Reply With Quote
  #6  
Old 08-28-2008, 12:30 AM
Wayne
Guest
 
Default Re: Inner classes

bugbear wrote:
> Roland de Ruiter wrote:
>> On 27-8-2008 17:34, bugbear wrote:
>>> katyusha wrote:
>>>>
>>>>
>>>> Can someone please guide me a link on java inner classes ?. I am
>>>> having a tough time getting the nuances of the access mechanism for
>>>> outer and inner class variables.
>>>>
>>>
>>> http://java.sun.com/docs/books/tutor...erclasses.html
>>>
>>> BugBear

>>
>> Probably OP should start reading one page earlier to get a completer
>> picture:
>> <http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html>.
>> This page refers to
>> <http://blogs.sun.com/darcy/entry/nested_inner_member_and_top>, which
>> gives an even more detailed description of the relations of the
>> various types of classes.

>
> Yeah - most likely - I just hit "java inner classes tutorial"
> in google, and cut 'n' pasted the first hit, which appears to be more
> than the OP did.
>
> BugBear


Actually that tutorial has an error. Despite what it says, all
nested ("static nested"), inner, or local classes do have
a trust relationship with the outer ("enclosing") class:

<sscce>
class Outer {
private int someNum = 17;

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

private static class Inner {
private void showNum () {
Outer outer = new Outer();
System.out.println( outer.someNum ); // displays 17
}
}
}
</sscce>

-Wayne
Reply With Quote
  #7  
Old 08-30-2008, 02:57 AM
Roedy Green
Guest
 
Default Re: Inner classes

On Wed, 27 Aug 2008 08:00:12 -0700 (PDT), katyusha <milon87@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>Can someone please guide me a link on java inner classes ?. I am
>having a tough time getting the nuances of the access mechanism for
>outer and inner class variables.


I have written some essays to help get you started. See
http://mindprod.com/jgloss/innerclasses.html
then follow the links at the bottom to the related essays.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Reply With Quote
  #8  
Old 08-30-2008, 02:51 PM
Wayne
Guest
 
Default Re: Inner classes

I was playing around with inner classes and was
hopping someone could explain the following. Given:

import java.lang.reflect.*;
public class NestedScope {
private int num = 17;

public static void main ( String [] args ) {
NestedScope outer = new NestedScope();
outer.demo();
}

private void demo () {
Inner inner = new Inner();
inner.showNum();
}

private class Inner {
private void showNum () {
System.out.println( num ); // Displays 17
}
}
}

This works fine when compiled and run. But, I was cleaning
it up when I was done and noticed that javac had created
an extra class file:

C:\Temp>del *.class

C:\Temp>javac NestedScope.java

C:\Temp>dir *.class
Volume in drive C has no label.
Volume Serial Number is XXXX-XXXX

Directory of C:\Temp

08/30/2008 02:28 PM 184 NestedScope$1.class
08/30/2008 02:28 PM 756 NestedScope$Inner.class
08/30/2008 02:28 PM 671 NestedScope.class
3 File(s) 1,611 bytes
0 Dir(s) 3,213,533,184 bytes free

C:\Temp>javap NestedScope$1
Compiled from "NestedScope.java"
class NestedScope$1 extends java.lang.Object{
}


C:\Temp>del NestedScope$1.class

C:\Temp>java NestedScope
17

C:\Temp>javac -version
javac 1.6.0_07

C:\Temp>

"NestedScope$1" has no fields, constructors, or methods of it's own.
As the above shows it isn't used at all when the program is run.

Comment out the object creation (the whole body) of the
"demo" method, and then re-compiling, and the extra class doesn't
get generated.

It is true that there is no legal reason why a Java compiler can't
create temporary files but you expect them to be deleted when
the compiler finishes.

The class file naming scheme was recently codified into the language
definition, so there can be conflicts with "real" anonymous classes.
To check that out I added this to the main method:

new Object() {public void foo(){System.out.println("foo");}}.foo();

Which produced this result:

C:\Temp>del *.class

C:\Temp>javac NestedScope.java

C:\Temp>dir *.class
Volume in drive C has no label.
Volume Serial Number is 549D-754B

Directory of C:\Temp

08/30/2008 02:42 PM 501 NestedScope$1.class
08/30/2008 02:42 PM 756 NestedScope$Inner.class
08/30/2008 02:42 PM 706 NestedScope.class
3 File(s) 1,963 bytes
0 Dir(s) 3,205,283,840 bytes free

C:\Temp>java NestedScope
17
foo

C:\Temp>

As you can see the pointless NestedScope$1.class was not generated
at all (I expected NestedScope$2.class) in this case!

What am I missing? I bet it is something obvious. Or is this
some sort of "bug"?

-Wayne
Reply With Quote
  #9  
Old 08-30-2008, 03:02 PM
Wayne
Guest
 
Default Re: Inner classes

Sorry, the import statement was left over from
some other experiments, and isn't really there.
Please remove that 1 line mentally from my post.

-Wayne
Reply With Quote
  #10  
Old 08-31-2008, 01:25 AM
tomaszewski.p
Guest
 
Default Re: Inner classes



Użytkownik "Wayne" <nospam@all.4me.invalid> napisał w wiadomości
news:48b9964e$0$4912$9a6e19ea@unlimited.newshostin g.com...
> I was playing around with inner classes and was
> hopping someone could explain the following. Given:

[...]
>
> This works fine when compiled and run. But, I was cleaning
> it up when I was done and noticed that javac had created
> an extra class file:
>

[...]

> 08/30/2008 02:28 PM 184 NestedScope$1.class
> 08/30/2008 02:28 PM 756 NestedScope$Inner.class
> 08/30/2008 02:28 PM 671 NestedScope.class
> 3 File(s) 1,611 bytes
> 0 Dir(s) 3,213,533,184 bytes free
>
> C:\Temp>javap NestedScope$1
> Compiled from "NestedScope.java"
> class NestedScope$1 extends java.lang.Object{
> }
>

[...]
As you can see the pointless NestedScope$1.class was not generated
> at all (I expected NestedScope$2.class) in this case!
>
> What am I missing? I bet it is something obvious. Or is this
> some sort of "bug"?
>
> -Wayne


Hi,

If you remove 'private' keyword in 'Inner' class, additional method is
not generaed.
Try to decompile 'NestedClass.class' and 'Inner.class' to get the answer why
additional $1 class is created.

Przemek

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:44 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.