생활코딩/생활코딩 Java

Java_method - 부록 - static

양상추상츄 2021. 10. 12. 20:57

https://www.youtube.com/watch?v=tcxf9epFD_U&t=749s 

static이 있으면 class method이고

static이 없으면 instance method이다.

 

 

package method;

class Print{
    public String delimiter;
    public void a() { //static이 없어야 인스턴스 메소드에서 사용할 수 있다.
        System.out.println(this.delimiter); //this.delimiter this는 인스턴스를 가리킴(t1)
        System.out.println("a");
        System.out.println("a");
    }
    public void b() {
        System.out.println(this.delimiter);
        System.out.println("b");
        System.out.println("b");
         
    }
    public static void c(String delimiter) {
        System.out.println(delimiter);
        System.out.println("b");
        System.out.println("b");
    }
}
public class staticMethod { 
     
    public static void main(String[] args) { // 실제 실행되는 메소드
//      Print.a("-");// 클래스의 소속으로써 실행되는 메소드 이 메소드실행시에는 static 필요
//      Print.b("-");
         
        // instance
        Print t1 = new Print(); //맨앞의 Print는 데이터 타입을 나타냄
        //t1은 Print의 분신
        t1.delimiter = "-";
        t1.a(); //메소드가 인스턴스의 소속일 경우 static을 빼줘야함
        t1.b();
        Print.c("$");
         
         
//      Print.a("*");
//      Print.b("*");
         
        Print t2 = new Print();
        t2.delimiter = "*";
        t2.a();
        t2.b();
    }
     
 
}