Error: identifier expected in Java

我只是一个虾纸丫 提交于 2019-11-27 07:18:17

问题


I am a new learner of Java. I learned some of the Java core concepts. I got the identifier expected error when run my following code:

class Sekar {
  public static int i,j,k;
  i = 900;
  static void max()
  {
    j = 100;
    if(i>j)
    {
      k=i;
    }
    else {
      k=j;
    }
    System.out.println("The maxmimum vale between"+i+"and"+j+"is :"+k);
  }               
  public static void main(String[] args) {
     max();       
  }
}

When I compile my code, I get the following error:

error: identifier expected
  i    =  900;
     ^
  1. Can any one explain why this error happens here?
  2. When I google about identifier expected error, I found that this error happens when variables are declared without datatype, but I declared that for all my variables i,j,k.
  3. When I redeclare the data type again while setting value to "i" like int i = 900 it works. Why does it?

回答1:


i = 900;

This is a statement in Java, it can be inside Constructor or method, or initialization block.

In your case, you may move that inside the max() method

When I re declare the data type again while setting value to "i" like int i = 900 it works. Why does it?

Here, you are declaring and assigning the value to the variable in the same time, same line.

Check here for more details and here about java statements, expressions

Statements

Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon (;).

  • Assignment expressions
  • Any use of ++ or --
  • Method invocations
  • Object creation expressions



回答2:


Hava a look at Java: Identifier expected :

i = 900;

is a statement as any other. You can't write statement anywhere. It must be in methods/constructors body. Initializing variable in declaration is called definition and is exception to this rule.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html

If you want to initialize static variable you can do it 2 (sane) ways: Initialize variable right where you are declaring it:

class Sekar {
  public static int i = 900, j,k;

  static void max()
  {
    j = 100;
    if(i>j)
    {
      k=i;
    }
    else {
      k=j;
    }
    System.out.println("The maxmimum vale between"+i+"and"+j+"is :"+k);
  }               
  public static void main(String[] args) {
     max();       
  }
}

or do it in static constructor:

class Sekar {
  public static int i, j,k;

  static {
    i = 900;
  }

  static void max()
  {
    j = 100;
    if(i>j)
    {
      k=i;
    }
    else {
      k=j;
    }
    System.out.println("The maxmimum vale between"+i+"and"+j+"is :"+k);
  }               
  public static void main(String[] args) {
     max();       
  }
}

Also, if you want to define a constant I recommend using final keyword.

j could be converted to local variable.

class Sekar {
  public static final int I = 900;

  static void max()
  {
    int k;
    int j = 100;
    if(I>j)
    {
      k=I;
    }
    else {
      k=j;
    }
    System.out.println("The maxmimum vale between"+I+"and"+j+"is :"+k);
  }               
  public static void main(String[] args) {
     max();       
  }
}



回答3:


What you probably want to do is this:

class Sekar {
    public static int i=900,j=100,k;
    static void max()
    {
        if(i>j)
        {
            k=i;
        }
        else {
            k=j;
        }
        System.out.println("The maxmimum vale between"+i+"and"+j+"is :"+k);
    }
    public static void main(String[] args) {
        max();
    }
}    

However I would discourage you from using static fields in this case. I would suggest you to make i, j and k parameters to your method. And give them descriptive names while you're at it.

Also note that k is not initialised explicitly and is therefore set to 0 by default, so your else clause is never reached.



来源:https://stackoverflow.com/questions/22594207/error-identifier-expected-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!