-
인스턴스 멤버와 정적 멤버자바 Java/문법 2022. 1. 7. 10:01
인스턴스 멤버: 객체 인스턴스마다 가지고 있는 필드와 메소드. 객체 소속된 멤버이므로 객체 없이 사용불가
class MyMath2 { long a, b; // 인스턴스변수 a, b만을 이용해서 작업하므로 매개변수가 필요없다. long add() { return a + b; } // a, b는 인스턴스변수 long subtract() { return a - b; } long multiply() { return a * b; } double divide() { return a / b; } // 인스턴스변수와 관계없이 매개변수만으로 작업이 가능하다. static long add(long a, long b) { return a + b; } // a, b는 지역변수 static long subtract(long a, long b) { return a - b; } static long multiply(long a, long b) { return a * b; } static double divide(double a, double b) { return a / b; } } class MyMathTest2 { public static void main(String args[]) { // 클래스메서드 호출. 인스턴스 생성없이 호출가능 System.out.println(MyMath2.add(200L, 100L)); System.out.println(MyMath2.subtract(200L, 100L)); System.out.println(MyMath2.multiply(200L, 100L)); System.out.println(MyMath2.divide(200.0, 100.0)); MyMath2 mm = new MyMath2(); // 인스턴스를 생성 mm.a = 200L; mm.b = 100L; // 인스턴스메서드는 객체생성 후에만 호출이 가능함. System.out.println(mm.add()); System.out.println(mm.subtract()); System.out.println(mm.multiply()); System.out.println(mm.divide()); } }
'자바 Java > 문법' 카테고리의 다른 글
접근 제한자와 Getter Setter (0) 2022.01.14 비트 논리 연산자(&, |, ^, ~) (0) 2022.01.04 은행 프로그램(예금,출금,잔고) - dowhile반복문과 scanner 활용 (0) 2022.01.04 열거 타입(Enumeration Type) (0) 2022.01.03 배열 복사하는 방법 - for, system.arrayCopy() (0) 2022.01.03