I had no problem passing the validation (IDEA makes it easy by underlining errors) so there's no attached code here. I just want to ask for some clarification about what the code is doing in this task. Why does the interface create an object of the class that implements it? Is this normal practice? It seems that the object HOBBY belongs to the interface in this case, so Dream.HOBBY works but would that mean you can't reference HOBBY without Dream?
Just looking for some explanation about what the code is doing
You must be signed in to leave a comment
4 August 2021, 13:41
The variable HOBBY in the Dream interface is static, so it is object independent. All variables inside an interface will automatically be public static whether you put that or not, in fact if you try to make them private or protected you will get an error. The interface does not need to be implemented in the class of any variables contained within the interface. In this code you could access HOBBY by the interface name, the class that implements the interface, or an instantiated object of the class:
4 August 2021, 14:55
Thank you. Just some additional questions: 1. Is the syntax Dream.HOBBY analogous to Color.RED (we've used that in the game projects)? 2. What is the value of Dream.HOBBY? Is it just the Hobby class's toString method (which is default right now)? 3. Line 14... should be implements, not extends, right? 4. So Hobby is a static class, which means that there is only one copy of it, right? You wouldn't create separate Hobby objects. I'm just a little confused then by the use of new Hobby(), which is how we create new objects of classes.
4 August 2021, 16:00
1) Probably. I don't recall all the code from the game tasks, but standard practice for public static fields is to name the variable in ALL CAPS. 2) A custom class can have any value that you want. In the HOBBY class code from the task, it does not have any class fields (the static INDEX isn't an instance field), so there isn't anything definitive to determine its value. This is because the task is just demonstrating specific points. In this task you could make 10 different HOBBY objects in main and then print each one out; they will all output the same thing even though they are different objects. 3) yes. That was a typo as I copied and pasted the task code then implemented it for the point i was making. If I don't run it through intellij first then small errors like that can occur. 4) An inner class that is static can be implemented without an instance of the outer class. If the Hobby class was not static then an instance of one could not exist without an object of the Solution class. So:
4 August 2021, 19:29
Thank you, I think I'm getting it better now.
5 August 2021, 21:40
The fact that the inner Hobby class is static means that the Hobby class is not part of any Solution object. But You can create as many new Hobby objects as you want! We could have written the Hobby class in a separate file, and then it wouldn't have to be static. I just think that for such a simple tiny program, it's more transparent to have the one or two classes you need inside a file.
5 August 2021, 21:47
PS: A static VARIABLE is that thing which there is only one, and all objects use the same one.