2

I know that java doesn't support static Top-Level classes, and only nestled classes can be static.

But, wherever I read about these nestled static classes, it says you have to initialize these classes with the "new" keyword.

Is this not instancing? if yes, how are we instancing "static" classes?

Does this mean we can have multiple instances of this class running simultaneously? Can we store these on variables just like non-static classes?

If this is true, then is the "static" modifier just a modifier used to access the nestled class without instantiating the container class?

Any Light shed on these questions are very welcome. Thanks!

EDIT: This question is different than the questions it has been marked duplicate against as I am NOT asking what the static modifier does to an object, nor am I asking if static classes exist. I know what static nestled classes are, and how static objects behave, but the static nestled class'es peculiar behaviour of being instantiatable by using the"new" keyword (which is used to create an instance of an object). Static classes can usually not be created instances of in OOP, and I was asking is this different in java. can you create instances of static classes in java.

Spacejet
  • 131
  • 1
  • 11
  • 2
    The class being static has nothing to do with whether it can be instantiated, it has to do with how it 's related to the class it's in – Stultuske Mar 21 '19 at 13:06
  • 1
    What do you mean, @Stultuske? Do you mean That being static in java is not the same as other oo programming languages? is it just a modifier to access the class? – Spacejet Mar 21 '19 at 13:07
  • The class is static, meaning it's linked to the class it's contained into. – Yassin Hajaj Mar 21 '19 at 13:08
  • 1
    I don't really understand, @YassinHajaj. are you saying that the class is static to be accessed without an instance of the container class? – Spacejet Mar 21 '19 at 13:09
  • yes, that is exactly it. – luk2302 Mar 21 '19 at 13:13
  • 1
    Can someone answer the question as an answer so that I can accept it? – Spacejet Mar 21 '19 at 13:14
  • Yes indeed, `new Upperclass.StaticInnerClass()` is used for example – Yassin Hajaj Mar 21 '19 at 13:17
  • @PranavBadrinathan - There is no need for that. This Q&A has been asked and answered before. – Stephen C Mar 21 '19 at 13:20
  • Where, @StephenC? I searched everywhere before asking this question! EDIT: Ok I see the answered question. – Spacejet Mar 21 '19 at 13:26
  • 1
    @StephenC, I have seen both of those questions, but none answered my question. My Question is : Why can static class'es instances be created, and these questions you have linked do not explain why! – Spacejet Mar 21 '19 at 13:29
  • 2
    The explanation is that your understanding of the meaning of `static class` is incorrect. And your inference that `new` shouldn't work is based on this incorrect understanding. The correct meaning of `static class` is explained in the first linked Q&A. Seeing a Q&A is not sufficient. You need to read and try to understand it as well! If you understood the linked Q&A, you would see why your objection to "instancing" a static class is ... well ... ill-founded. – Stephen C Mar 21 '19 at 13:34
  • @StephenC Ok Thanks – Spacejet Mar 21 '19 at 13:37
  • @YassinHajaj Meaning that it *isn't* linked to the class it's contained in. – user207421 Mar 22 '19 at 10:25
  • @user207421 Could you elaborate why it isn't linked to the class? – Yassin Hajaj Mar 22 '19 at 10:36

0 Answers0