• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

inner classes

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, a static inner class (local or anonymous) can only access static members of the enclosing class.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Veena , Bindesh...
http://softwaredev.earthweb.com/java/article/0,,12082_859381,00.html - helped me understand these concepts clearly.....hope it helps you too.
 
Bindesh Vijayan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Everybody.
Nupur I have just now saved that page to my folder.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's one more for those that just can't get enough questions about inner classes.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
pie. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic