728x90
package method;
public class MethodTest2 {
//구현
public void disp() {
System.out.println("non=static method");
}
public static void output() {
System.out.println("static method");
}
public static void main(String[] args) {
MethodTest2.output();
//static라서 그냥 호출해도 출력이 가능
MethodTest2 a = new MethodTest2();
a.disp();
//static이 아니라서 new연산자를 이용해 메모리에 객체를 생성후
//.연산자로 출력
}
static로 메소드를 만들면 메모리에 객체가 생성되어 있는 상태라서 클래스.메소드()로 호출이 가능하지만
static 없이 만들면 메모리에 객체가 생성되있지 않은 상태이기 때문에
new연산자로 객체를 생성후에 호출할 수 있다.
728x90
'JAVA' 카테고리의 다른 글
10진수를 2,8,16진수로 변환하는 법 (0) | 2020.09.03 |
---|---|
계산기(메소드, switch) (0) | 2020.09.03 |
[오류]Syntax error on token "class", @ expected (0) | 2020.08.31 |
클래스와 변수 (0) | 2020.08.31 |
배열 연습문제03 (0) | 2020.08.12 |