I don't understand the Java memory structure Who wants to tell me?

Asked 2 years ago, Updated 2 years ago, 23 views

I made a static method outside the main method And I made a static variable in the method, but there was an error... From what I learned, static is stored in the data area before the program is executed Why is there no static variable in the static method? I know it's meaningless, but I don't know why it doesn't work I really looked it up on Google for a few days, but I can't find an answer. Actually, I asked an intellectual, but he just said no, but he didn't tell me why, so I'm asking here ㅠ<

java

2022-09-20 19:03

1 Answers

In Java, the static keyword can only be preceded by a member of the class. Because this is a rule, if you break this rule, an error will occur.

As you ask, adding static to a variable in the method is a rule violation because the variable is a local variable, not a member of the class. Therefore, errors occur and are not created. As mentioned above, the rule is that the static keyword can only be preceded by a member of the class.

You may be confused because the rules are different for each language.


2022-09-20 19:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.