2017-10-30 4 views
-1

コードを実行しようとすると、ブール型エラーが発生します。ブーリアンは静的ではありえないと言います。答えは何か。Javaブール静的エラー

package csd; 
class Uti { 
    public static void main(String[] args) { 
     boolean result; 

     result = Sample.foo() && Sample.bar(); 
      System.out.printf("result%b%n",result); 
    } 

    class Sample { 

     public static boolean foo() { 
      System.out.println("foo"); 
      return true; 
     } 

     public static boolean bar() { 
      System.out.println("bar"); 
      return false; 
     } 
    } 
} 

エラーメッセージ:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method foo cannot be declared static; static methods can only be declared in a static or top level type

+3

正確なエラーメッセージを表示できますか?このエラーはどこにありますか?あなたのUtiクラスが公開されていない理由はありますか? – Stultuske

+2

スレッド "main"の例外java.lang.Error:未解決のコンパイルの問題: \t fooを静的宣言することはできません。静的メソッドはstaticまたはtopレベルの型でしか宣言できません –

+1

'Sample'クラスは内部クラスであり、そのクラスには静的メソッドを含めることができないというエラーメッセージが表示されます。 – Eran

答えて

4

あなたSampleクラスの静的があなたのエラーを解決します作る:

class Uti { 
    public static void main(String[] args) 
    { 
    boolean result; 

    result = Sample.foo() && Sample.bar(); 
    System.out.printf("result%b%n",result); 
    } 

    static class Sample { 

    public static boolean foo() { 
     System.out.println("foo"); 

     return true; 
    } 

    public static boolean bar() 

    { 
     System.out.println("bar"); 

     return false; 
    } 
    } 
} 

は、トップレベルのクラスでも動作します作る:

class Sample { 

    public static boolean foo() { 
    System.out.println("foo"); 

    return true; 
    } 

    public static boolean bar() 

    { 
    System.out.println("bar"); 

    return false; 
    } 
} 

class Uti { 
    public static void main(String[] args) 
    { 
    boolean result; 

    result = Sample.foo() && Sample.bar(); 
    System.out.printf("result%b%n",result); 
    } 
} 
1

ウェルコードにコンパイルエラーがあります。コードで次のように親タイプに静的修飾子を追加することで問題を解決できます。

public static void main(String[] args) 
     { 
     boolean result; 

     result = Sample.foo() && Sample.bar(); 
       System.out.printf("result%b%n",result); 
     } 
    static class Sample { 

    public static boolean foo() { 
     System.out.println("foo"); 

    return true; 
    } 

    public static boolean bar() 

    { 
    System.out.println("bar"); 

    return false; 
    } 
    }