• Post Reply Bookmark Topic Watch Topic
  • New Topic

Where we need to use the Static inner class nested classes  RSS feed

 
ramakrishna rayudu
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi....

can you give me any idea in which case i need to write the class with in another class.
and what is the difference between static inner class and nested class.
can you give me any example where i need to use these concepts in real world.

Thanks in Advance,
Ramakrishna Rayudu.
 
Adam Miller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look here. Hope this link helps

http://download.oracle.com/javase/tutorial/java/javaOO/nested.html
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Adam Miller
 
Adam Miller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch Adam Miller


Hello Campbell Ritchie. Thank you for your greetings
 
satish.ice Reddy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static nested class is just like any other outer class, as it doesn't have access to outer class members.

Just for packaging convenience we can club static nested classes into one outer class for readability purpose. Other than this there is no other use case of static nested class.

Example for such kind of usage, you can find in Android R.java (resources) file. Res folder of android contains layouts (containing screen designs), drawable folder (containing images used for project), values folder (which contains string constants), etc..

Sine all the folders are part of Res folder, android tool generates a R.java (resources) file which internally contains lot of static nested classes for each of their inner folders.

Here is the look and feel of R.java file generated in android: Here they are using only for packaging convenience.



You can find similar kind of nested class java questions
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ramakrishna rayudu wrote:can you give me any idea in which case i need to write the class with in another class.

Simple answer: where it makes sense.

If you have a class that only has a context within the domain of another one - and a good guide to that is when its name only makes sense when added to its outer one: eg, AbstractMap.SimpleEntry.

It's probably worth mentioning that interfaces can also be nested. The above class, for example, implements a nested interface called Map.Entry.

and what is the difference between static inner class and nested class
can you give me any example where i need to use these concepts in real world.

You have to be a bit careful with terms like 'nested' and 'inner', because they mean different things to different people (although I believe the JLS is quite explicit about their use).

There are 4 specific types of class that people often refer to as "inner":
  • 1. Static nested classes - As satish said, these are just like any other class; they just happen to be defined inside another one.
  • 2. Non-static nested classes - These are far less common, and are often used to create an Adapter, where the implementation of the inner class is dependant on its outer one. They are also usually private. Java 'collection' classes use them a lot to define Iterators and "views" (eg, Map.keySet()) to the backing collection.

  • These two together, comprise "nested" classes.

  • 3. Local classes - This is a class defined inside a method. I'm afraid I can't give you an example because I've never written one in anger (and I've been using Java for 12 years), but I believe the tutorial link you were given has one.
  • 4. Anonymous classes - An unnamed class, usually used to create a temporary object. Again, the tutorial link has an example.

  • I believe that any of the above types, except #1, can correctly be called "inner", so its best to be specific when you're talking about them.

    HIH

    Winston
     
    Mike Simmons
    Ranch Hand
    Posts: 3090
    14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:There are 4 specific types of class that people often refer to as "inner":
  • 1. Static nested classes - As satish said, these are just like any other class; they just happen to be defined inside another one.
  • 2. Non-static nested classes - These are far less common, and are often used to create an Adapter, where the implementation of the inner class is dependant on its outer one. They are also usually private. Java 'collection' classes use them a lot to define Iterators and "views" (eg, Map.keySet()) to the backing collection.

  • These two together, comprise "nested" classes.

    These two, together, comprise member classes. Nested classes include all of 1-4.

    I agree on all other points. I found a real-world use for a named local class exactly once in my career so far, and I was pleasantly surprised. But I'll be very surprised to find another. In Scala, by contrast, it happens frequently. Go figure.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Simmons wrote:I found a real-world use for a named local class exactly once in my career so far, and I was pleasantly surprised. But I'll be very surprised to find another.

    Do you mind telling us where? I've often thought that a method requiring some repetitive action might be able to use one; but I've never found a case where a local class leaps out at me.

    BTW: Thanks for the correction; I must have mis-read Josh Bloch (it's been a while ), but what you say makes sense.

    Winston
     
    Mike Simmons
    Ranch Hand
    Posts: 3090
    14
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sure - it was in a record-parsing method, where the objective was to extract a group of fixed-length fields from a single fixed-length record. Something like this:

    Where A, B, C are just different field names, with different fixed lengths.

    Obviously there are other ways that this could have been refactored, but as I only needed the RecordFieldReader in that one method, this seemed to work well for me. An anonymous class wouldn't work well because there wasn't an existing class or interface with a read) method with the signature I needed, and I needed to be able to call the method outside the class. A static member class would have worked fine, but would have had a wider scope than it really needed. And I would have had to add a bytes field, and a constructor, just to provide access to the local variable bytes that the local class already has access to. The parsing rules were specific to this particular record type, and I didn't find a need to generalize the class any further.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Simmons wrote:Sure - it was in a record-parsing method, where the objective was to extract a group of fixed-length fields from a single fixed-length record....
    A static member class would have worked fine, but would have had a wider scope than it really needed. And I would have had to add a bytes field, and a constructor, just to provide access to the local variable bytes that the local class already has access to. The parsing rules were specific to this particular record type, and I didn't find a need to generalize the class any further.

    Very nice. And, as far as I can see, flawless design logic. Cheers, Mike. I've bookmarked this for future reference.

    Winston
     
    Kumar Raja
    Ranch Hand
    Posts: 550
    2
    Hibernate Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi All,

    I don't mean to hijack this thread, but since my question is related to static nested class and inner class, thought of asking my question here instead of starting a new thread.

    In my code, which is a file generator program, I have used some non static inner classes for value objects, comparators etc. The file generator program is ideally a message listener and will be start creating the files based on the event it receives.

    So far, my code is working fine without any issues. However, when we ran FindBugs, it complained about the usage of non static inner class and recommends the usage of static nested class. I agree that outer class uses my inner classes and inner classes do not have any explicit reference of outer class.

    Given that code is working, I'm not sure, if I should change it to static nested class as recommended by FindBug. These inner classes will not be referred by any other external program and hence I took the liberty of creating these inner classes just like any normal class, without reference to outer class.

    eg.

    InnerClass clazz = new InnerClass();

    instead of
    OuterClass.InnerClass clazz = new OuterClass().new InnerClass();

    What could be the unforseen complications, If I decide to keep the inner class as non static.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kumar Raja wrote:I don't mean to hijack this thread, but since my question is related to static nested class and inner class, thought of asking my question here instead of starting a new thread.

    To be honest, I'm not sure either, so we'll let it go as long as ramakrishna doesn't mind. It may also help to explain one of his questions.

    In my code, which is a file generator program, I have used some non static inner classes for value objects, comparators etc. The file generator program is ideally a message listener and will be start creating the files based on the event it receives.

    Well, a "listener" isn't necessarily a good fit for an inner class, since its definition may be independent of any class, but an adapter may well be - particularly if it implements an interface.

    So far, my code is working fine without any issues. However, when we ran FindBugs, it complained about the usage of non static inner class and recommends the usage of static nested class. I agree that outer class uses my inner classes and inner classes do not have any explicit reference of outer class.

    If it's a proper inner class, then it should need a reference to an object of its outer class. That's the whole point.

    Ask yourself this: Is your "inner" class dependant on:
    (a) the implementation of its outer class?
    (b) a specific object of its outer class?
    (c) a broader specification - usually an interface (not strictly necessary)?

    If your answers are yes, then the chances are that an inner class is exactly what you want. If not, it should probably be static. It should be added that an inner class only saves the business of supplying its outer object as a parameter but, since it's a potential source of errors, it's generally best to use the most concise form possible.

    Winston
     
    Kumar Raja
    Ranch Hand
    Posts: 550
    2
    Hibernate Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Winston,

    Actually now I have more questions on this topic, after seeing your reply. Instead of confusing the original poster, I will ask this question in an another thread which I opened here

     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kumar Raja wrote:Actually now I have more questions on this topic, after seeing your reply. Instead of confusing the original poster, I will ask this question in an another thread which I opened here

    Good idea.

    Winston
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!