I have two questions for the program below.
If you write ST_Test.testMethod(2);
in the main class, the method is executed, but if you write ST_Test.testMethod(2);
in the ST_Test class, which is a static class, you get a compilation error.
The error details are as follows.Why does this happen?Is there a big difference between writing in the main class and writing in the static class?
estmain.java:45:Error: <identifier> missing
ST_Test.testMethod(2);
^
testmain.java:45:Error: Incorrect start of type
ST_Test.testMethod(2);
^
2 errors
This time, the ST_Test class is not the main class, so
static class ST_Test
"I wrote ""class ST_Test"" instead of writing ""static"" would it be okay to just draw ""class ST_Test""?"
// Programs that understand the difference between instance and static methods
public class Testmain {
public static void main(String[]args) {
ST_Test.testMethod(2);
}
static class ST_Test {
public static String S_STR = "S_STR";
public String str = "str"; intn;
ST_Test.testMethod(2);
public static void testMethod(inti){
System.out.println("i="+i);
}
}
}
Run Environment
You cannot write a method call in a Java class.
static class ST_Test{
public static String S_STR = "S_STR"; // This is a property, so OK
public String str = "str"; intn;// This is also a property, so it's OK.
ST_Test.testMethod(2); // No method calls here
}
I'm trying to do something I can't do, so I don't know what I want to do, but here's what I modified to go through the compilation:
public class Testmain{
public static void main(String[]args) {
ST_Test.testMethod(2);
ST_Test.hoge();
}
static class ST_Test {
public static String S_STR = "S_STR";
public String str = "str"; intn;
public static void hoge() {
ST_Test.testMethod(1);// I can't write directly, so I'll wrap it up with the hoge method.The argument is also changed from 2 to 1 to make it easier to distinguish it from the other call.
}
public static void testMethod(inti){
System.out.println("i="+i);
}
}
}
Writing ST_Test.testMethod(2); in the main class causes the method to run
Although it is described as , strictly speaking, ST_Test.testMethod(2);
is written within the main
method in Testmain
, so it is not a sudden method call in the class.
Is there a big difference between writing in the main class and writing in the static class?
Therefore, the difference is whether you were making a method call in the method or not, regardless of whether you are in the static
class or not.
Would it be okay if I only drew class ST_Test instead of writing static?
Now that the compilation is through, try removing static
from static class ST_Test
.
Then I got the following error.
Testmain.java:8:error:Illegal static declaration in inner class Testmain.ST_Test
public static String S_STR = "S_STR";
^
modifier 'static' is only allowed in constant variable declarations
Testmain.java:11:error:Illegal static declaration in inner class Testmain.ST_Test
public static void hoge() {
^
modifier 'static' is only allowed in constant variable declarations
Testmain.java:15:error:Illegal static declaration in inner class Testmain.ST_Test
public static void testMethod(inti){
^
modifier 'static' is only allowed in constant variable declarations
Therefore, static
seems to be necessary.
Regarding question 2, static is required.Because this program is running without generating objects in the Testmain class, non-static inner classes that cannot be used until the Testmain class is instantiated cannot be called in this flow.
If you want to write the whole thing in non-static, for example:
public class Testmain{
public static void main(String[]args) {
Testmain main = new Testmain();
ST_Test_test=main.new ST_Test();
st_test.testmethod(2);
}
private class ST_Test{
public void testmethod(inti){
System.out.println("i="+i);
}
}
}
"The theme is ""Program to understand the difference between instance and static methods,"" so I think it is better to try to grasp various codes until you are satisfied."At first (some people) tend to write all the static programs, but it's not a good habit, so it's better to get into the habit of writing code while understanding the relationship between classes and instances.
© 2024 OneMinuteCode. All rights reserved.