Hello,
I have done task, but I don't understand how is it possible that we create instance of static class.
I thought that static class doesn't allow to create its' instance.
Could someone explain it to me?
Szymon Pawlikowski
Level 19
How is it possible that we can create instance of static class?!
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Szymon Pawlikowski
3 January 2022, 08:12
If someone has the same question as me at this stage, there is CG lesson about it on level 6 in lesson 7. So be patient :)
0
Gellert Varga
29 December 2021, 21:13useful
There recently was a very similar question from another student.
At the time, I felt that the questioner was probably confused (in relation to the static class instantiating) by the following:
- in CG lessons we learn that on the one hand there are instance variables that will construct the instances. Each object will have one such instance variable, so there will be as many "public int age" variables as there are objects.
- On the other hand, there are static variables, which do not become part of the objects, there is only one of them in the class, and the objects share this one variable. There is only one of the variable "public static int count", even if you have created a lot of objects from the class.
But the static inner class is a different thing, it is not a static variable.
You can easily make a lot of instance of it. (instance = object.)
It may not be a completely accurate approach, but I tend to think of static inner classes as if they were completely separate independent classes.
This is a free online IDE where you can test your code at any time, even after CG validation has been done. You can test anything:
https://www.jdoodle.com/online-java-compiler/
+1
Lisa
29 December 2021, 08:25useful
As I do not know the code you refer to I just can tell you that this of course is possible. Why that is the case probably is to much info at this point.
The static modifier indicates that the inner class holds no reference of the outer class. That way objects of a static inner class are far easier to instantiate and hence used a lot by CG. You learn more about that in the multithreading quest.
+1
Szymon Pawlikowski
29 December 2021, 12:08
Thank You for Your answer, it helped me for now.
I couldn't add code because task was completed.
+1