Static variable
:Static variables are also known as Class variables.
A variable which is declared as static is called static variable. It cannot be local. You can create a single copy of static variable and share among all the instances of the class. Memory allocation for static variable happens only once when the class is loaded in the memory.
스태틱변수=클래스변수
:만약 클래스 내에서 모두 공유하고 싶은 변수가 있다면 스택틱 변수로 선언해야한다.
스태틱변수는 클래스멤버 변수를 의미한다.
public class MemberVariable {
public static Random rd = new Random();
public static int num=0; //Static variable
public static void method0() {
System.out.println("method0: "+num);
num = rd.nextInt(100);
System.out.println("method0: "+num);
}
public static void method1() {
System.out.println("method1: "+num);
num = rd.nextInt(100);
System.out.println("method1: "+num);
}
public static void main(String[] args) {
method0();
System.out.println("num:"+num);
method1();
System.out.println("num:"+num);
}
}
'java' 카테고리의 다른 글
Java/Selection Sort/선택정렬 (0) | 2020.01.28 |
---|---|
Java/Local Variable and Static Variable/ 지역변수와 스태틱변수의 비교 (0) | 2020.01.28 |
Java/Local Variable/지역변수 (0) | 2020.01.28 |
Java/Swap values and arrays (0) | 2020.01.27 |
Java/return문; (0) | 2020.01.27 |