2014-11-25 1 views
0

이것은 매우 간단한 프로그램입니다. 나는 새로운 클래스를 만들었습니다, 그리고 아미 옆 새로운 클래스에서 호출 할 수있는 새로운 방법을 정의하는 것이다. 당신은 방법에 방법을 선언 할 수 없습니다자바 실행 프로그램

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Syntax error on token(s), misplaced construct(s) 
    Syntax error on token "void", @ expected 
+6

showSomething()을 정의해야합니다 ** main() 외부 ** ** –

+0

코드에서 수행 할 작업을 원하십니까? 다른 메소드 안에 메소드가 있습니다. 왜? –

답변

3

오류입니다.

변경이 :

public static void main(String[] args) { 
    int number = 1; 
    public void showSomething(){ 
     System.out.println("This is my method "+number+" created by me."); 
    } 

} 

또한 showSomething()

public static void main(String[] args) { 
    int number = 1; 
    showSomething(); // call the method showSomething() 
} 
public static void showSomething(){ 
    System.out.println("This is my method "+number+" created by me."); 
} 

하려면 static이다 static main() 때문에 선언한다. 다른 static method에서 static methods 만 호출 할 수 있습니다.

+1

메소드'showSomething'은 정적이어야합니다. –

+0

@ArnaudDenoyelle 실수로 인해 반가 웠습니다. 편집 내 대답 – Joe

2

:이 프로그램을 실행하면

public class MyClass { 

    public static void main(String[] args) { 
     int number = 1; 
     public void showSomething(){ 
      System.out.println("This is my method "+number+" created by me."); 
     } 

    } 
} 

는하지만, 오류가 발생합니다.

은 이런 식으로 작업을 수행 : 당신이 main() 여기에 다른 방법 안에 방법을 선언하고 있기 때문에

public class MyClass { 

    public static void main(String[] args) { 
    int number = 1; 
    showSomething(number); 
    } 

    public static void showSomething(int number){ 
    System.out.println("This is my method "+number+" created by me."); 
    } 
} 
1
public class MyClass { 

public static void main(String[] args) { 
    new MyClass().showSomething(); 
} 

public void showSomething(){ 
     int number = 1; 
     System.out.println("This is my method "+number+" created by me."); 
} 

} 
-1
public class MyClass { 

    public static void main(String[] args) { 
     int number = 1; 
     showSomething(number 
    } 

    public void showSomething(int number){ 
     System.out.println("This is my method "+number+" created by me."); 
    } 
} 
0

public class MyClass { 


    public static void showSomething(int number){ 
      System.out.println("This is my method "+number+" created by me."); 
    } 

    public static void main(String[] args) { 
     int number = 1; 
     showSomething(number); 
    } 
} 
0

당신은 당신의 주요 내부의 방법을 만들 수 없습니다, (주 외부 메소드를 정의)이 같아야합니다. 대신 다음과 같이 수행 클래스에서

public class MyClass { 

    public static void main(String[] args) { 
     showSomething();//this calls showSomething 

    } 

    public void showSomething(){ 
     int number = 1; 
     System.out.println("This is my method "+number+" created by me."); 
    } 
} 

이 프로그램을 실행하는 주요 방법이있다. 동일한 레벨에서 프로그램에서 사용하려는 다른 메소드 나 변수가 있습니다.

+0

관심을 가져 주셔서 감사합니다. – Amin