wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner classes" Watch "inner classes" New topic
Author

inner classes

Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Inner classes have accessibility to all the member variables of enclosing class(or outer class) including private data fields..Does this mean inner class can access all the data fields of enclosing class in a similar way as method of enclosing class?
Veena


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Well, a static inner class (local or anonymous) can only access static members of the enclosing class.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Veena,
Yes, inner classes have access to the private data of the enclosing class. However, as anthony pointed out, an inner class that is also static can only access the static members of the enclosing class.
My mock exam has seven questions on inner classes and the answer page has detailed explanations of the answers. If you would like to try all seven, then go to the topic exam page and click on the inner classes exam.
The following URL will take you directly to the topic exam page, but if you want to bookmark the exam then you should only use the URL contained in my signature. It is the only page that is certain to stay in one place.
Topic Exams, July 8 version
[ July 10, 2002: Message edited by: Dan Chisholm ]

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 34
I read the following from JLS 8.1.2
"An inner class is a nested class that is not explicitly or implicitly declared static"
Does this mean that a nested class that is declared static is not an inner class.
And what does this mean
"Nested classes that are not inner classes may declare static members freely, in accordance with the usual rules of the Java programming language"(JLS 8.1.2).Any codes to explain this.
Pls.help
Bindesh Vijayan
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Bindesh,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you.


SCJP Tipline, etc.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
The semantics may be confusing, but the idea is simple. A class that is a member of an enclosing class is a nested class. The member class can be static or non-static. If it is static, then it can behave like a top level class. For example, it can be instantiated without first creating an instance of the enclosing class. For example, the following code would work if the nested class is static.
Outer.Inner oi = new Outer.Inner();
Howerver, if the Inner class is non static, then you have to first create a new instance of the outer class as follows.
Outer.Inner oi = new Outer().new Inner();
Both static and non-static nested classes have access to the private members of the enclosing class and vice versa.
A non-static nested class (inner class) can not declare static members. Try to compile the following.

I think I'll put that one on my mock exam. Thank you for the idea.
[ July 11, 2002: Message edited by: Dan Chisholm ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Bindesh:
"An inner class is a nested class that is not explicitly or implicitly declared static"
Does this mean that a nested class that is declared static is not an inner class.

That's correct. A nested class that is declared as static is called a "top level nested class," not an inner class.

And what does this mean
"Nested classes that are not inner classes may declare static members freely, in accordance with the usual rules of the Java programming language"(JLS 8.1.2).Any codes to explain this.

That simply means that a top level nested class is allowed to declare static members. This is something that inner classes can not do.
Corey
nupur dhawan
Ranch Hand

Joined: Jun 26, 2002
Posts: 71
Veena , Bindesh...
http://softwaredev.earthweb.com/java/article/0,,12082_859381,00.html - helped me understand these concepts clearly.....hope it helps you too.


Nupur. <br />SCJP2.
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 34
Thanks Everybody.
Nupur I have just now saved that page to my folder.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
To illustrate the rules associated with
static and non-static nested classes, I have added the following question to my mock exam.

I will post the answer as the next message in this thread.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
The following is the remark that is associated with the answer to the question posted earlier.

A non-static nested class is called an inner class. An inner class can not declare a static member unless it is a compile time constant. Even though f is final, it does not have a value at compile time and therefore it causes a compilation error. Member variable g also causes a compiler error because it is static. The static initializer of NonStaticInner causes a compiler error because inner classes can not declare static initializers.

The answer to the above question is as follows.

Compiler error at line 12.
Compiler error at line 13.
Compiler error at line 15.

I have not yet uploaded a new version of the exam to the web. I will probably do that on or before Monday.
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Thanks to everyone for the detailed explanation about inner classes.
But I meant with the exception of static inner classes & even anonymous inner classes.Inner classes which are neither static nor anonymous have same kind of access control to the member variables of enclosing class as method of enclosing class right?I thought this way coz it is easy to remember which data fields of enclosing class can be accessed by an inner classes.For example inner class can't access static fields of enclosing class in a similar way as a nonstatic method of enclosing class.I didn't find difference between the way both inner class & method of enclosing class access the datafields of enclosing class.Just wanted to confirm it.
Veena
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
You are correct. A static nested class can not access a non-static member of the enclosing class. I just added the following question to my mock exam to further emphasize that point.

The answer is g.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Here's one more for those that just can't get enough questions about inner classes.
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
Veena, as far as I can tell, non-static, non-anonymous (is that even a word? ) inner class have same kind of ability to access its outer class' stuff as any non-static member method that belong to the outer class. I think the difference between a non-static member method of a outter class and a non-static non-anonymous member inner class of the same outter class is design issue.
Let me set this up. let us say that a computer is an object. A method would be like a CPU, since its function is so vital and relavent to how the computer (object) is behaving. While a screen is like an inner class, because while a screen is still part of what we define as computer, stricly speaking we don't quite need it and also the fact is that the screen is self does its own stuff that the computer don't care about (things like how to line up the liquid crystal if you got an LCD screen). So you see, while both the CPU and the screen are part of a computer, CPU are just more "relavent" to computer and screen, and screen has some processes that only makes sense to itself and so it is more self contained then CPU.
Does this make any sense? Correct me if I am wrong.


Let us be showered in the light of confusion!
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
If a class has a need for a helper class and if the class requires access to the private members of the helper class or if the helper class requires access to the private members of the enclosing class, then the helper is a good candidate for being declared as a nested class.
In addition to the above, there are other motivations for declaring a class as a nested class. For example, if a class requires a unique helper class and if the helper is not needed by any other class, then the helper would be a good candidate for being declared as a nested class.
The above are only a couple motivations for declaring a class as a nested class. I'm sure that others could post additional good reasons.
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 34
Veena , I agree with some part of what u said.But it is here where i disagree

For example inner class can't access static fields of enclosing class in a similar way as a nonstatic method of enclosing class."

To me it appears that this not true.Compile and run the following codes..

Correct me if I'm wrong.
Dan I could'nt understand about helper classes.Can you please ,provide some code snippet, to make it clear to me..
But there is one importance ,that I know, which the inner class solves, and I would like to share it here .
The problem of Multiple Inheritance which is only partly solved by interfaces.And the rest is solved by the uasage of inner classes.e.g.

This way the developer class inherited from many concrete class which without the use of inner classes was impossible(atleast to me).This is just one of the use,there is another use also of giving the implementation of the interfaces and keeping them implementation hidden from outside world.
To be more clear with usage of inner classes read "Thinking in Java" By
Bruce Eckel
Thanks
Bindesh Vijayan
[ July 13, 2002: Message edited by: Bindesh Vijayan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inner classes
 
Similar Threads
Inner class - private field access
Anonymous class
Violation of Inner-Outer rules???
Accessibility of private methods in inner classes
? inner class or local class member