I am having a problem with my code in Eclipse IDE for Java 2020 where when I put my code to run, it doesn't do anything, I am doing a online course but I cant ask question, because the class are recorded before (3 years ago)
Some of the things on the code is in Brazilian Portuguese, but the things that are doesn't matter, they are only class notes, here is my code:
You have everything commented in the main method so i assume that you are asking about part below. You have to make variable and initialization static in order to run it without creating object of your class:
Otherwise you have to create object of your class in your main method, and keep everything as it is:
Maciej Liso wrote:. . . . You have to make variable and initialization static . . .
Afraid that is incorrect. The correct object‑oriented way to do things is to get into a context of using objects as soon as posssible; things static don't constitute part of an object.
LS: welcome to the Ranch Thank you for trying the code button, but you didn't quite get it right: put [/code] after the block. I corrected it for you and doesn't it look better. Unfortunately I can no see some poor indentation which can cause you problems if anything goes wrong with your flow of execution.
I misspoke. The code does not have a static block. I guess the term I meant is a code block. I think a code block outside of any methods is sort of like a constructor that is executed when a new instance of the class is created.
It's an instance initialiser in that case. It never runs until an instance is created, and if I remember correctly, it behaves as if it were copied into the beginning of the constructor just after super(...); (or this(...);). The gory details to prove me wrong are in the Java® Language Specification (=JLS).
To me the code looks like extended Hello World so to get it to run the easiest way is to include it in the main by removing the braces on line 48 and 49. Maybe later in his career Luis Felipe can learn all about instances and static things but I guess it's too early for that.