• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

usuage of static method and static inner class

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the need to use static inner class in java?
is it possible to achieve same functionality by static method?
when declared any non static method inside static inner class, is it implicitly static?



 
Rancher
Posts: 43024
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

A.Inner.mymethod(); // not allowed cannot make static reference, why mymethod is not static as it is inside static class


A static method is something entirely different than a static class. Being part of a static class does not make a method static.

See http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html about static nested classes.
 
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raju pal wrote:what is the need to use static inner class in java?



A static inner class, is an inner class that doesn't have any access to an outer class instance. Not sure what you need by "need" though -- but sometimes you do want an instance of an inner class that doesn't require an reference to an instance of an outer class.

raju pal wrote:
is it possible to achieve same functionality by static method?



Not sure what you mean by this -- can you show us an example?

raju pal wrote:
when declared any non static method inside static inner class, is it implicitly static?



Of course not. How would instance methods be able to access it's this reference, if the compiler treated it as a static method.


BTW, since something tells me that there is a core misconception here -- I will hold off answering the rest until clarification is provided.

Henry
 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the question is: Why use a static nested class when I can just use a regular class with static methods?

Java Tutorials wrote:
"Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience."



As far as I can tell a static nested class doesn't give you any sort of special functionality. But I can imagine that "packaging convenience" can be a pretty big deal for larger applications.
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tyson Lindner wrote:I think the question is: Why use a static nested class when I can just use a regular class with static methods?



No, the question is also this.

OP wrote:when declared any non static method inside static inner class, is it implicitly static?



 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tyson Lindner wrote:I think the question is: Why use a static nested class when I can just use a regular class with static methods?



Ok, you're probably right as regards the following part.

OP wrote:is it possible to achieve same functionality by static method?



Please, ignore what I said.
 
Henry Wong
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tyson Lindner wrote:I think the question is: Why use a static nested class when I can just use a regular class with static methods?

Java Tutorials wrote:
"Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience."



As far as I can tell a static nested class doesn't give you any sort of special functionality. But I can imagine that "packaging convenience" can be a pretty big deal for larger applications.




What about the ability to have instances of the static inner class? How is that to be achieved via using static methods of the top-level class?

Henry
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
What about the ability to have instances of the static inner class? How is that to be achieved via using static methods of the top-level class?



Care to give an example and show why that can't just be done with regular classes?
 
Henry Wong
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tyson Lindner wrote:

Henry Wong wrote:
What about the ability to have instances of the static inner class? How is that to be achieved via using static methods of the top-level class?



Care to give an example and show why that can't just be done with regular classes?




Oh, I see... the tutorial is referring to porting the static inner class to its own top level class (and not moving the functionality to the top level class).


In that case, I guess it is like advanced packaging, but I wouldn't call it as merely a convenience -- as you can do things with access between the static inner class, and its top-level class, that can't be done with two top level classes.

Henry
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are many advanced level differences probably related to encapsulation, memory management etc and I don't know about them. But the following is what I was playing with two weeks back.

I *think* static inner classes can be inherited. This implies that the methods declared in such nested classes can be overridden meaning run time polymorphism would be into play for such methods.

But static methods are only inherited. They cannot be overridden. They are redefined. So there is no runtime polymorphism.

 
Henry Wong
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chan Ag wrote:
I *think* static inner classes can be inherited. This implies that the methods declared in such nested classes can be overridden meaning run time polymorphism would be into play for such methods.



All inner classes, static or otherwise, can be extended from each other (and with top-level classes). Besides dealing with the coordinating between the inner class' super class, and the inner class' outer class (which can get complicated), the rules are pretty much the same..... oh, and besides the issue that inner classes (non-static) are not allowed to have static methods.

Henry
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Henry.

Yes, I had kind of figured this two weeks back but I was not sure today. And I will probably forget the rules again after 10-15 days. :-)

Ermmm... Regardless, what I said does count as a difference between the static methods and nested classes.

So there are things that we can do with the nested classes that we can't with a regular class' static methods.
Though I understand that it could be a difference that has no practical use or something. I was just 'experimenting'...

<Guessing>I think nested classes must provide better encapsulation than static methods in some cases.</Guessing>
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, no.

It is probably pointless to compare the fact that static methods cannot be overridden with the fact that nested classes can be extended ( all non final classes can be extended ).

I was probably comparing, like they say, apples and oranges..

 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chan Ag wrote:
Ermmm... Regardless, what I said does count as a difference between the static methods and nested classes.



It seems like you're just showing the difference between static methods and non-static methods, it doesn't matter if the enclosing class is a nested class or top-level class.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What was confusing to me is that when I think of "static class" I think of a class with all static members. But java doesn't even really have that, it just has regular classes that may or may not have all, or some, of their members static. Supposedly other languages do have explicitly static classes in that sense.

So "static inner classes" aren't at all static classes in the above sense. IMO I wish they would use a different keyword than static to avoid that type of confusion.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tyson, take this as a question cause that's what it is.

What would be your approach if you needed a method in an outer class for which you
wouldn't want to depend on the outer class instance and your implementation would
also want to allow run time polymorphism for different implementations of this method.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also let us say that the outer class is a final class. :-)
 
Henry Wong
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tyson Lindner wrote:What was confusing to me is that when I think of "static class" I think of a class with all static members. But java doesn't even really have that, it just has regular classes that may or may not have all, or some, of their members static. Supposedly other languages do have explicitly static classes in that sense.

So "static inner classes" aren't at all static classes in the above sense. IMO I wish they would use a different keyword than static to avoid that type of confusion.




This is likely the confusion of the OP -- especially, the part where the OP mentions "when declared any non static method inside static inner class, is it implicitly static?".


In my opinion, I think that it is an okay overloaded use of the static keyword. Just like static methods don't have access to the this reference, instances of static inner classes don't have access to an outer class reference. I agree that it is a bit of a stretch -- but I also kinda understand what the Java developers were thinking when they created the concept of nested classes.

Henry
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chan Ag wrote:
What would be your approach if you needed a method in an outer class...



The point of what I'm saying is that since the static inner class doesn't use an outer class instance at all, it won't be *needed* in that class. That class can be in a separate file, and its methods are accessed with the same functionality as if it were a static inner class. There won't be a difference in runtime polymorphism as long as the methods have the same modifiers.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<strikethrough>But it wouldn't be a static class.

And hence I guess it can't take the advantage of
having the class loaded only if it's instance is created.

Would that be right?
</strikethrough>

I'm not sure about this. I need to read about class loaders.

Its in my list of things to do. You are probably right.

Ignore this post.
 
Henry Wong
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tyson Lindner wrote:
The point of what I'm saying is that since the static inner class doesn't use an outer class instance at all, it won't be *needed* in that class. That class can be in a separate file, and its methods are accessed with the same functionality as if it were a static inner class. There won't be a difference in runtime polymorphism as long as the methods have the same modifiers.




As mentioned, this is not completely true. Static inner classes does have unique access capabilities that can't be replicated by moving them to a top level class. For example, this won't work if you move the static inner classes, and keep the modifiers unchanged...



And even if you change the modifiers, such as making the icA and icB variables to be public, you are doing so to only allow the compile. It is not exactly the same functionality, as you are now allowing other classes to also be able to access those instance variables.

Henry
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
As mentioned, this is not completely true.



Yeah I forgot about visibility. That's more to do with static inner classes being inner classes in general rather than using the static keyword though.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's such a fine example, Henry. Thanks. God knows why it didn't strike me that although nested classes can't access the instance members and instance of the outer class for obvious reasons, there is a lot more an outer class can have that the nested classes can access.

Actually I think in specific instances like the way ThreadPoolExecutor handles RejectedExecutionException through nested classes, like how the HashMap handles bucket entries, nested classes provide good encapsulation. I mean it's easy to visualize them as being associated with the containing class and yet the nested classes are free to be extended. So the code is loosely coupled and yet provides good encapsulation. I think that's an awesome thing.

There is probably more to nested classes but they must be too advanced topics for me. Advanced, at least till I've read about 'em...

But, thanks...

Tyson, I somewhat agree with some of your points but I think packaging right is an extremely important thing, even if we assume that it is the only facility the nested classed provide. And like we've seen, I think nested classes provide more than just an additional packaging alternative.
 
Ranch Hand
Posts: 262
4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think one really good use of the static inner class is while you're constructing objects of a class that takes too many optional parameters.

If you code the constructors using the telescoping pattern, the approach doesn't scale well. If you use the Java Beans pattern, your objects might be in an inconsistent state part way through the construction process and such objects can never be immutable.

Comes the builder pattern to rescue. And for that you need the static inner classes.

Note : I've referred to the book Effective Java, Edition 2, for compiling this post.
That and this post.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic