Java memory, Static, psvm
✅ Method, Stack, Heap
✔️ Method
- class
✔️ Stack
- methods
- program is run
✔️ Heap
- Instance created
new- class instance, array
💡 Method
- shared data to run program
class: field, method, constructor…static: shared among instances- literal
💡 Stack
methodsrun in main classlocal variables,parameter, method return values…- LIFO
main() start➡️method1 start➡️method2 startmethod2 end➡️method1 end➡️main() end- method in class 🆚 method in main class
- method in class:
methodarea, shared data for creating instances - method in main class:
stackarea
💡 Heap
Instance,arraynewinstance- GC is run in heap area
if value is not referenced in
heaparea, GC would free address space- student class 🆚 student 1 instnace
newstudent 1 instance is made from student class- student class: method area
- student 1 instance made from student class: heap area
- student 2 instance made from student class: heap area
- student 1, 2 instance will be each allocated address space in heap area
- however, student class is used commonly for student 1, student 2
1
2
3
4
5
6
7
8
9
10
11
public static void main(String[] args) {
method1();
}
static void method1() {
Data data1 = new Data(10);
method2(data1);
}
static void method2(Data data) {
}
main() start- ➡️
method1 start - ➡️
new data1in heap allocated address space - ➡️
method2 start - ➡️
method2 end - ➡️
method1 end - ➡️
new data1in heap is not referenced anymore - ➡️ GC clean
- ➡️
main() end
✅ Static
student class로student 1, 2, 3...등 만들건데, 인스턴스간에 공유하는 변수를 지정할 수는 없을까?- 1️⃣
staticis to share among instances - 2️⃣
staticvariables are saved inmethod memory, only once- normally, instance is created in
heap - however, bc
staticis shared among instances, it is saved inmethod memory - only saved once(shared among instance)
- normally, instance is created in
3️⃣
staticvariables can be used without creating instance- 👎🏻 Before(without static)
1
2
3
4
5
6
7
8
9
public class Data1 {
public String name;
public int count; //👎🏻 not static
public Data1(String name){
this.name = name;
count++; //when data instance is created, ++
}
}
1
2
3
4
5
6
7
8
9
10
11
12
public class DataCountMain1 {
public static void main(String[] args) {
Data1 data1 = new Data1("A");
System.out.println("data: " + data1.count);
Data1 data2 = new Data1("B");
System.out.println("data: " + data2.count);
Data1 data3 = new Data1("C");
System.out.println("data: " + data3.count);
}
}
- ✔️ result
1
2
3
data: 1
data: 1
data: 1
- when instance is created,
countis0 thus, making
count++will be1- 👍🏻 After(with static)
1
2
3
4
5
6
7
8
9
public class Data3 {
public String name;
public static int count; //👍🏻 implement static
public Data3(String name){
this.name = name;
count++;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
public class DataCountMain3 {
public static void main(String[] args) {
Data3 data1 = new Data3("A");
System.out.println("data: " + Data3.count); //static
Data3 data2 = new Data3("B");
System.out.println("data: " + Data3.count); //static
Data3 data3 = new Data3("C");
System.out.println("data: " + Data3.count); //static
}
}
- ✔️ result
1
2
3
data: 1
data: 2
data: 3
staticvariables can be used without creating instanceData3.count⭕️data1.count❌static int countis shared among instancedata 1, 2, 3- thus every time instance is created,
기존 count++ - because
static int countis shared among instance, saved inmethodpart of memory- only once saved in
methodmemory
- only once saved in
- 🆚
String name(not static) will be saved inheappart of memory- saved
3times inheapmemory - (created instance
3times)
- saved
Local variable 🆚 Instance variable 🆚 Class variable
1
2
3
4
public class Data3 {
public String name; //class field, instance variable
public static int count; //class field, class variable, static
}
- Local variable: saved on memory
stack, short life - Instance variable: saved on memory
heap, cleaned by GC - Class variable: saved on memory
method, longest life as variable
✅ Static Method
1️⃣
staticmethod can be used without creating instance👎🏻 Before(without static)
1
2
3
4
5
public class DecoUtil1 {
public String deco(String str){
return str;
}
}
1
2
3
4
5
6
7
8
public class DecoMain1 {
public static void main(String[] args) {
String s = "hello";
DecoUtil1 utils = new DecoUtil1(); // 👎🏻 create instance
utils.deco(s); // 👎🏻 then use method
}
}
- 👍🏻 After(with static)
1
2
3
4
5
public class DecoUtil2 {
public static String deco(String str){ //static
return str;
}
}
1
2
3
4
5
6
7
public class DecoMain2 {
public static void main(String[] args) {
String s = "hello";
DecoUtil2.deco(s); //call static method without creating instance
}
}
- 2️⃣
staticmethod can only usestatic fieldandstatic method - 🆚
instancemethod can use bothstatic,instance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class DecoData {
private int instanceValue; //instance variable
private static int staticValue; //static
public static void staticCall(){ //static
staticValue++; //static can use static
//instanceValue++; //🔴compile error, static can only use static
//instanceMethod(); //🔴static can only use static
}
public void instanceMethod(){
staticValue++; //🟢instance can use both static and not static
instanceValue++;
staticCall();
}
}
- 🤨 Why
staticmethod can only usestatic? staticis created inmethodstaticcan be used without creating instance- thus, if
staticcalls aninstance field or method, theinstancemight not have been created yet!
1
2
3
4
5
6
7
8
public class DecoDataMain {
public static void main(String[] args) {
DecoData.staticCall(); //static method is called from class, do not need instance to be created
DecoData data = new DecoData();
data.instanceMethod(); //instance method is called from instance, after creating instance
}
}
✅ psvm Main()
main methodis alsostatic
1
public static void main(String[] args) {}
- 1️⃣ can use
main()method without creatingmain instance main()is used to start the programs- 2️⃣ methods called in
main()should also bestatic
✅
✅
This post is licensed under CC BY 4.0 by the author.

