This article was written for people who were programming in other languages and would like to program in Java.
A scope is a usable range. Not limited to variables, the scope is called the scope. This time, I will give an example of the scope of variables. Example 1) As you can see, you can't bring chain variables directly from main. In other words, the scope of i in chain is in chain, and the scope of i in main is in main. Example 2) The value declared in the if statement can be used only in the if statement. If you want to use it elsewhere, there is no problem if you change the value declared in the if statement so that it is operated in the if statement.
There are two variables, global variables and local variables. Local variables are values (= scope is finite) that can only be used within the range as mentioned above. Global variables, on the other hand, are variables with unlimited scope. Global variables do not exist in Java, but they can be in the same state as global variables. The description method is the description of public static variables. Example) Global variables can be referenced from anywhere, so you can change their values from other classes. It has a huge impact, so use it only when you need it and use local variables as much as possible.
I mentioned that global variables are described as public static variables, This is because it extends the scope where both public and static can be used. I will also explain these two points.
public is one of the access modifiers. I will give you one by one. ・ Public has no restrictions. · Protected only allows access from within a class or a subclass that inherits from the class. -If nothing is attached, it can be accessed from all classes in the same package. -Private can only be accessed from inside the class.
Of these, the movement was confirmed except for the protected variable, but since the protected movement was unexpected, it is a condition other than protected once. Confirmation program
practice.java
class Main {
public static String pubstr = "public";
protected static String prostr = "protected";
static String str = "None";
private static String pristr = "private";
public static void main(String[] args) {
System.out.println(pubstr); //public
System.out.println(str); //None
System.out.println(pristr); //private
check();
Sub subclass = new Sub();
subclass.sub();
}
public static void check() {
System.out.println(pubstr); //public
System.out.println(str); //None
System.out.println(pristr); //private
}
}
class Sub {
public void sub() {
Main m = new Main();
System.out.println(m.pubstr); //public
System.out.println(m.str); //None
System.out.println(m.pristr); //Error because private cannot be obtained
}
}
static static means that you can also use static methods (methods with static). Show a concrete example.
practice.java
class Main {
public int ni = 10;
public static int si = 7;
int di = 999;
public static void main(String[] args) {
System.out.println(ni); //Error because ni is not a static variable
System.out.println(si); //Since si is a static variable, 7 is output
}
public void check(String[] args) {
System.out.println(ni); //10 is output because check is not a static method
System.out.println(si); //7 is output because check is not a static method
}
}
In this way, only variables with static can be used in methods with static.