This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Inner Class - Clarification

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I was taking a mock test today from "Marcus Green", I had this question.

Which are true about Inner Classes?

The first choice was, "Inner classes can be static". I ignored this answer as I considered it false. BUT THIS WAS INCLUDED IN THE ANSWER LIST FOR THIS QUESTION.

My Consideration was: Nested class can be static but not inner class. You call a static nested class as Top level nested class or nested class.

So, Please clarify me in this. Can we say a static nested class as an Inner class? Thanks!
[ October 30, 2004: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The static modifier cannot be applied to the local class,means if class
come under method,constructor or block.If the class come under Top class
as a member class then static can be applied to that class.And all of the class modifiers can be applied to member class.If any thing wrong correct
me.
Thanks and Regds

Imagine IT,Think IT,Done IT

 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vadivel ! Thanks for your reply.

I am very clear with the syntax and semantics of Inner Class and Nested Class. What confused me here, is the terms that they use in the test questions.

All I wanted to ask is the following:

1. Is, Any class which comes under a Enclosing class as the enclosing class' member , a Inner Class ?? To put it simply, "Should I consider, Static Nested Class, an Inner class?"

2. I saw one choice considered as true in "Marcus Green" test. An anonymous class can never be a local class. Is this true?

What if I create an anonymous class within a method? Even in that case is it not a local class?

Please reply! I am sorry, to have not given the exact question or the link, which have uttered these statements. The problem is I dont have the exact question number nor the URL.

Thanks!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may like to read Corey's TipLine article.
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry! Thanks!

It is mentioned in Corey's article that, A class inside another class, which is Static is called Nested class and is not an Inner class.

So, where ever I see a statement like "Inner Class can be static", I will consider it "false" ... right?
[ October 30, 2004: Message edited by: Manikandan Jayaraman ]
 
vadivel mani
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manikandan,
your question 1.you can consider.but again it may be static or
not static.So there is no condition it should be static.
your question 2.Anonymous class never be local class.It is true.
Because a Local class should have a name.

Are you clear now?

Imagine IT,Think IT,Done IT.
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in the way that programmers tend to use "nested class" and "inner class" interchangebly. In Marcus Green's book:

What Is a Nested Class?
Generally speaking, a nested class is a member of another class. However, it is common to speak of the static members as nested and the nested classes that are members of instances of the enclosing class as inner classes. Indeed, you are most likely to read about inner classes only, but the more general term is nested classes.
Brogden and Green, Exam Cram Java 2 Programmer, p.88

 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even at Sun, we *used* to use the word "inner" class to refer to ANY nested class, as J Borderi said. But this is no longer correct! A static nested class is NOT an inner class, according to the formal definitions used today.

The current exam will never use the term "inner" to refer to a static nested class. Some nested classes can be called "inner" classes, but not ALL "nested" classes can be called "inner" classes.

Does that help?

cheers,
Kathy
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Barry]: You may like to read Corey's TipLine article.

This article seems to imply that nested and inner classes are two separate things - which is not the case. All inner classes are also nested classes. Static nested classes (also known as static member classes) are nested classes, but not inner classes.

From the top: all classes are either top-level classes, or nested classes. Nested classes are either static member classes, or inner classes. Inner classes are either (nonstatic) member classes, local classes, or anonymous classes.

[Brogden/Green]: Generally speaking, a nested class is a member of another class.

I think this is misleading. Local and anonymous classes are not members of another class, but they are considered both inner and nested classes. Generally, a nested class is a class defined inside another class. How and where it's defined will determine what specific type of nested class you're dealing with.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to read Sun Java Tutorial: Implementing Nested Classes It gives a clear definition for nested, static nested and inner classes.

Joyce
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gives a clear definition for nested, static nested and inner classes.


Clear, but wrong. A good example of the confusion that's existed in Sun over this topic.

Definition: A nested class is a class that is a member of another class.

Except when it's not a member - as for local and anonymous classes. Which are later cited in the tutorial as examples of nested classes, so it's not just that the tutorial contradicts other (more official) Sun sources - it contradicts itself. :roll:

Definition: An inner class is a nested class whose instance exists within an instance of its enclosing class and has direct access to the instance members of its enclosing instance.

Except when it has no instance. As when a local or anonymous class is defined inside a static method, for example.

I believe the tutorial is trying to explain new concepts as simply as possible, by simplifying them. As a result, several statements are not really very accurate if you want a complete picture of how various nested types work.
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointing out the contradictions, Jim.

I checked out the JLS for local class. The definition for nested class in the Java tutorial contradicts the one in JLS.


Extracted from JLS 14.3:
A local class is a nested class that is not a member of any class and that has a name.



Joyce
[ October 31, 2004: Message edited by: Joyce Lee ]
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All, Thanks for your links and points.

Kathy! When Sun is no longer referring Static Nested Class as an Inner Class, there is no other doubt in my mind

As I said before, the terms confuse me but NOT their Syntax and Usage Thanks again!
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!