I'm studying Java, and I'm writing an article to output what I've learned and to establish my knowledge. I will describe what I have learned separately as follows.
-Java variable declaration / initialization / data type (cast and promotion) ・ Type conversion -Variable Scope ・ Character string operation (in preparation) ・ Array operation (in preparation) ・ Operator (in preparation) ・ Conditional branch (in preparation) ・ Repeat processing (in preparation) ・ About class (in preparation) ・ Abstract class (in preparation) ・ Interface (in preparation) ・ Encapsulation (in preparation) ・ About the module (in preparation)
The scope of a variable is the valid range of the defined variable.
Variables are basically inaccessible outside the declared location. Variables declared within a method are valid only within that method. This is called a local variable.
test.java
public class Main {
public static void main(String[] args) {
int one = 1;
{
int two = 2;
}
System.out.println(one + two);
}
}
The above program will result in an error. This is because the inner is called outside the scope of the inner. The program will work if you do the following.
test.java
public class Main {
public static void main(String[] args) {
int one = 1;
{
int two = 2;
System.out.println(one + two);
}
}
}
→
3
This is because one is called inside two. One can also be used inside two.
** Variables defined in scope cannot be used if they are out of scope ** Also, as shown below, the if statement has a scope inside the {}, so if it deviates from that, the variables defined in the if statement cannot be accessed.
Main.java
class Main{
public static void main(String args[]){
int n = 5;
if (n == 5) {
String seikai = "n is 5";
}
System.out.println(seikai);
}
}
Execution result ↓
seikai cannot be resolved to a variable
It can be executed by calling seikai in if as shown below.
Main.java
class Main{
public static void main(String args[]){
int n = 5;
if (n == 5) {
String seikai = "n is 5";
System.out.println(seikai);
}
}
}
Execution result ↓
n is 5
The same applies to the for statement.
Main.java
class Main{
public static void main(String args[]) {
for (int i = 5; i <= 10; i++) {
System.out.println(i); //Executable
}
System.out.println(i); //Not executable
}
}
Execution result ↓
i cannot be resolved to a variable
It can be executed by calling i in for as shown below.
Main.java
class Main{
public static void main(String args[]) {
for (int i = 5; i <= 10; i++) {
System.out.println(i);
}
}
}
Execution result ↓
5
6
7
8
9
10
In addition to the local variables mentioned above, there are also member variables. Variables declared outside the methods of a class are called member variables (fields). Member variables can be referenced outside the method but within the same class. Also, Java doesn't want to manipulate data directly from the outside, so if you want to reference a variable from another class, you can do this by using modifiers.
`Modifier: Determines where a class and its members are accessible. There are private, protected, public, etc. ``
I will write more about this later.