2013-08-01 2 views
9

프로그래밍에 익숙하지 않고 객체를 인스턴스화 할 때 어디서 잘못되었는지 알고 싶습니다. 아래 코드는 다음과 같습니다자바에서 객체를 인스턴스화하는 방법은 무엇입니까?

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) 
    { 
     Sample myTest = new Sample(); 
     System.out.println(c); 
    } 
} 
+0

변수'c'는'Sample' 메소드의 범위에서 접근 가능합니다. 이 범위에서 액세스하려고했습니다. –

답변

15

코드에서 더 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); 
+2

알다시피, 네 말이 맞아, 이해가 안되지만, 이봐, 배우고있어! – user2640722

+1

사실, 예! 그게 내가하고 싶은 일이야! Awesomesauce! – user2640722

1

,하지만 당신의 CALSS 이름과 메소드 호출이 잘못되었습니다 :

당신이 그런 짓을하려고 단지 방법 일뿐입니다. 인스턴스를 작성할 수 없습니다. 당신이 클래스로 샘플하고자하는 경우,

int sample = Sample(3); 

을 클래스로 정의 - 만 실행합니다.

경우에 따라 방법이 정적이 아니므로 정적 메서드 Main에서 직접 액세스 할 수 없습니다. 정적으로 만들어 액세스 할 수 있도록하십시오. 아니면 그냥 테스트의 새로운 인스턴스를 만들고 그것을 사용 -

Testing testing = new Testing(); 
int sample = testing.Sample(3); 
1

샘플 클래스없는 그것을 당신은 new 키워드를 올바르게 인스턴스화

class Sample { 
int a; 
int b; 

Sample(int a, int b) { 
    this.a = a; 
    this.b = b; 
} 

public int sum() { 
    return a + b; 
} 
} 

public class Testing { 
public static void main(String[] args) { 
    Sample myTest = new Sample(1, 2); 
    int sum = myTest.sum(); 
    System.out.println(sum); 
} 
} 
1

샘플 메서드는 결과를 얻고 어디서나 그것을 사용합니다.

public static void main(String []args) 
{ 
    int myTest = Sample(4555);//input may be any int else 
    System.out.println(myTest); 
} 
1

이렇게하면됩니다.

public class Testing{ 
public int Sample(int c) 
{ 
    int a = 1; 
    int b = 2; 
    c = a + b; 
    return c; 
} 
public static void main(String []args) 
{ 
    // Creating an Instance of Testing Class 
    Testing myTest = new Testing(); 
    int c =0; 
    // Invoking the Sample() function of the Testing Class 
    System.out.println(myTest.Sample(c)); 
} 
2

실제로 개체를 만들고 싶지 않습니다.

코드 스 니펫에서 두 개의 숫자를 추가하는 Sample이라는 '메소드'를 실행하고 싶습니다. 그리고 자바에서는 메서드를 인스턴스화 할 필요가 없습니다. 개체는 class의 인스턴스입니다. 메서드는이 클래스에있는 동작입니다.

컴파일 된 코드를 실행할 때 JAVA가 자동으로 클래스의 인스턴스를 만들고 실행하기 위해 main() 메서드를 찾으면 사용자가 명시 적으로 아무 것도 인스턴스화 할 필요가 없습니다.

public class Testing{ 
    private int sample(int a, int b) { 
     return a + b; 
    } 
    public static void main(String[] args) { 
     int c = sample(1, 2); 
     System.out.println(c); 
    } 
} 

참고 :

아마 당신은 다음을 수행 할 것이 일반적으로 대문자와 소문자와 클래스 이름과 메소드 이름을 시작하는 연습을 인정 것 같이 내가 sampleSample을 변경, 따라서 Testing이 그 앞면에 올 바릅니다.

관련 문제