class Variable{//생성자 정의와 호출
	private int c = 300;
	int d = 400;
}

public class VariableTest {
	//멤버 변수
	//private 는 같은 클래스 영역에서는 출력가능
	private int a = 100;
	private int b = 200;


	public static void main(String[] args){
		VariableTest v = new VariableTest();
		System.out.println(v.a);
		System.out.println(v.b);
		System.out.println("==================");
		Variable v2 = new Variable();
		/*
		System.out.println(v2.c);
		같은 클래스의 영역이 아니기 때문에 호출이 안됨
		private 접근 제한으로 호출 불가
		*/
		System.out.println(v2.d);
	}
}

100

200
==================
400


public class ReferenceParameter {// 참조 호출 Call by reference
	// 멤버 메소드
	// 메소드 호출방식 : 주소를 전달해서 메소드를 호출
	// call by reference
	public void increase(int[] n) {
		for (int i = 0; i < n.length; i++)
			n[i]++;
	}

	// 메인 메소드
	public static void main(String[] args) {
		// 배열 생성
		int[] ref1 = { 100, 800, 1000 };

		System.out.println("ref1[0] : " + ref1[0]);
		System.out.println("ref1[1] : " + ref1[1]);
		System.out.println("ref1[2] : " + ref1[2]);
		System.out.println("===================");
		ReferenceParameter rp = new ReferenceParameter();
		rp.increase(ref1);

		for (int i = 0; i < ref1.length; i++)
			System.out.println("ref1[" + i + "] : " + ref1[i]);
	}
}

ref1[0] : 100

ref1[1] : 800
ref1[2] : 1000
===================
ref1[0] : 101
ref1[1] : 801
ref1[2] : 1001


class ValueMethod{//값에 의한 호출 방식
	//y 랑 x는 상관이 없다
	void changeInt(int y){
		y=10; //전달된 데이터 7 -> 10
		System.out.println("y = "+y);
	}
}
public class MethodTest08 {
	public static void main(String[] args){
		//changeint 메서드를 호출하기 위해서 ValueMethod 객체 생성
		ValueMethod vm = new ValueMethod();
		int x=7;
		System.out.println("함수 호출 전 x-> " +x);
		//vm으로 ValueMethod 클래스의 changeint 메서드를 호출한다
		vm.changeInt(x);
		System.out.println("함수 호출 후 x-> " +x);
	}

}

함수 호출 전 x-> 7

y = 10
함수 호출 후 x-> 7


public class ValueParameter {// 인자 전달방식 값호출(call by value
	// 멤버 메소드
	// 메소드 호출 방식 : 값을 전달해서 메소드 호출(Call by Value)
	public int increase(int n) {
		++n;
		return n;
	}

	// 메인 메소드
	public static void main(String[] args) {
		int var1 = 100;
		// 객체 생성
		ValueParameter vp = new ValueParameter();
		int var2 = vp.increase(var1);
		//var1 으로 했기에 int n이 100이 되면서
		//증감식으로 101이 됨(var2가)
		System.out.println("var1 : " + var1 + ", var2 : " + var2);
	}
}

var1 : 100, var2 : 101


+ Recent posts