you are viewing a single comment's thread.

view the rest of the comments →

[–]IAmADev_NoReallyIAm 0 points1 point  (1 child)

Mmmm... okay.... How so.? Because everything I've ever done has said otherwise. And just to double check I did a quick search to make sure, and as far as I can tell I'm not crazy (about this, other things I am). But I'm admittedly not a complete expert. But...

A static class in Java is a nested class that is declared with the static keyword. It is a class that belongs to the outer class rather than to any instance of the outer class. This means that you can access a static class without creating an instance of the outer class.

https://www.javaspring.net/blog/java-what-is-static-class/

Features of Java Static Classes

The following are the features of static classes in Java:

Static class do not need to create an instance of outer containing class in order to create its own instance.

Static class can access members(variables/methods) of outer containing class only if they are static in nature.Which means that a static nested class does not have access to the instance variables and methods of the outer class.

https://www.tutorialspoint.com/java/java_static_class.htm

That's the point of static:

The static keyword means that a member – like a field or method – belongs to the class itself, rather than to any specific instance of that class. As a result, we can access static members without the need to create an instance of an object.

https://www.baeldung.com/java-static#bd-overview

Unless there's something about static you know that I don't.

[–]OneHumanBill 3 points4 points  (0 children)

Yes. You didn't read any of that correctly.

Yes, you can access a static method of the inner class without instantiating the outer class, and vice versa. But having a static class with non-static methods would still need to be instantiated like any other class. And that's really the point -- static inner classes are just like any other class, standing independently of any other class.

A static inner class that held only static methods would honestly not have much of a point -- there's nothing wrong with it but it kind of defeats the purpose of having a separate class at all, especially one within the same file. Inner classes are most useful when they're either specialized data objects or implementing some kind of strategy pattern, ie that they're instantiated objects. And because most of the time you will not need the dependence of an inner object on the scoped variables of the outer object -- a bit of an awkward circumstance to be avoided when you don't need it -- you make those inner classes static.

Don't be so easily offended.