코드에서 더 Sample
클래스가 없습니다. 당신이 선언 한 것은 개인적인 방법입니다. 현재 조각으로
// private method which takes an int as parameter and returns another int
private int Sample(int c)
{
int a = 1;
int b = 2;
c = a + b;
return c;
}
는, 당신은
Testing
클래스를 인스턴스화하고
Sample
방법의 사용을 확인해야합니다. 클래스 정의 앞에 키워드
(이 경우
class Testing
)이옵니다.
public class Testing{
private int Sample(int c)
{
int a = 1;
int b = 2;
c = a + b;
return c;
}
public static void main(String []args)
{
Testing t = new Testing(); // instantiate a Testing class object
int result = t.Sample(1); // use the instance t to invoke a method on it
System.out.println(result);
}
}
하지만 정말 이해가되지 않습니다, 당신의 Sample
방법은 항상 3
를 반환합니다. ,
Testing myTest = new Testing();
int result =myTest.Sample(1); //pass any integer value
System.out.println(result);
변수'c'는'Sample' 메소드의 범위에서 접근 가능합니다. 이 범위에서 액세스하려고했습니다. –