【问题标题】:Netbeans Java illegal public static void with compiling?Netbeans Java非法公共静态无效与编译?
【发布时间】:2015-09-10 19:49:40
【问题描述】:

下面会在public static void DemoMethod() 中引发错误。为什么?

 package demoblock;

  /**
  *
  * @author coleen
  */
  public class DemoBlock {

  /**
  * @param args the command line arguments
  */
  public static void main(String[] args) {

    System.out.println("Demontrating block scope");

    int x = 1111;
    System.out.print("in first block x is" + x);        
    {
        int y = 2222;
        System.out.println(" In second block x is " + x);
        System.out.println(" In seond block y is " + y); 
    }

    {
        int y = 3333;
        System.out.println(" In third block x is " + x);
        System.out.println(" In third block y is " + y);
        DemoMethod();
        System.out.println(" After method x is " + x);
        System.out.println(" After method block y is " + y);            
        System.out.println(" At the end x is " + x); 

        public static void DemoMethod()
        {

         int x = 8888;
         int y = 9999;
                 System.out.println(" In demoMethod x is " + x);
                 System.out.println(" In DemoMethod block y is " + y);                    

        }    
    }

【问题讨论】:

  • 你不能把一个方法放在另一个方法里面。还要注意,您的括号在提供的代码中是不平衡的。 (最后你至少错过了一个。)

标签: netbeans static void


【解决方案1】:
/** * * @author coleen */
public class DemoBlock
{

    /**
     * @param args
     *            the command line arguments
     */
    public static void main( String[] args )
    {

        System.out.println( "Demontrating block scope" );

        int x = 1111;
        System.out.print( "in first block x is" + x );

        {
            int y = 2222;
            System.out.println( " In second block x is " + x );
            System.out.println( " In seond block y is " + y );

        }

        {
            int y = 3333;
            System.out.println( " In third block x is " + x );
            System.out.println( " In third block y is " + y );
            DemoMethod();
            System.out.println( " After method x is " + x );
            System.out.println( " After method block y is " + y );

            System.out.println( " At the end x is " + x );
        }
    }

    public static void DemoMethod()
    {
        int x = 8888;
        int y = 9999;
        System.out.println( " In demoMethod x is " + x );
        System.out.println( " In DemoMethod block y is " + y );
    }

}

【讨论】:

    猜你喜欢
    • 2017-11-15
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    相关资源
    最近更新 更多