Results 1 to 19 of 19
  1. #1
    chandresh is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default error Identifier expected?

    Hi

    Some 10 years ago I used to write Java programs effieciently, then stopped due to some circumstances.
    Now trying to recap java again.

    My PC has Win 7 (32 bit) with JDK 1.7.0_03 installed and have also put the path of "bin" folder in the PATH variable, other wise javac was not working.

    Anyways, I wrote a simple code to test and it gave me some error which I could not understand WHY?
    Please advice as it gives me "error <identifier> expected at line 9

    1. public class tst2 {
    2. public static void main(String[] args) {
    3. System.out.println("HI");
    4. }
    5.
    6.
    7. public class tstinner {
    8. int a1, b1;
    9. a1 = b1 = 6;
    10. }
    11.
    12.}

    I have put the type "int" and then initialise the var a1 & b1 in a single line which is valid I guess, then why is it giving me the error?
    thanks

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: error Identifier expected?

    The following compiles OK:

    Java Code:
    public class Tst2 {
        public static void main(String[] args) {
            System.out.println("HI");
        }
    
        public class TstInner {
            int a1, b1;
            
            TstInner() {
                a1 = b1 = 6;
            }
        }
    }
    The difference is that the initialisation of a1 and b1 occurs within a constructor. You cannot try and assign the result of evaluating b1=6 to a1 except in a method, constructor or initialisation block. Of these a constructor is the most obvious place.

    I guess the main point is that foo=bar=42 does not mean "assign 42 to foo and bar" as you might have supposed. It means something like "evaluate bar=42 and assign the result to foo".

    -----

    When you post code don't include the line numbers. And use the "code" tags: put [code] at the start of the code and [/code] at the end. Your code will also be more readable if you follow java coding standards and use capital letters to begin classes - like TstInner.

  3. #3
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: error Identifier expected?

    Quote Originally Posted by pbrockway2 View Post
    The following compiles OK:
    I guess the main point is that foo=bar=42 does not mean "assign 42 to foo and bar" as you might have supposed. It means something like "evaluate bar=42 and assign the result to foo".
    Does bar equal 42 after that statement is executed?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: error Identifier expected?

    Also at javaprogrammingforums.com

    Please, if you are going start a discussion in multiple places, post links so that everyone who might want to contribute to the discussion can see what else is being said. Also be aware that some will not respond for fear of wasting their time on a question that has been adequately dealt with elsewhere.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: error Identifier expected?

    Quote Originally Posted by awinston View Post
    Does bar equal 42 after that statement is executed?
    Yes. Provided it is executed (ie it is part of valid Java code that compiles). bar gets initialised as a side effect of foo getting initialised.

    Initialising things outside of constructors and methods is rather restricted. For instance you also cannot say:

    Java Code:
    foo = 40 + 2;
    except in a method or constructor. There are probably exact specifications for such things in the JLS, but as a general rule: put code - outside of simple this_variable=that_value assignments - into methods and constructors.
    Last edited by pbrockway2; 08-15-2012 at 03:21 AM.

  6. #6
    chandresh is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default Re: error Identifier expected?

    Quote Originally Posted by pbrockway2 View Post
    The following compiles OK:

    Java Code:
    public class Tst2 {
        public static void main(String[] args) {
            System.out.println("HI");
        }
    
        public class TstInner {
            int a1, b1;
            
            TstInner() {
                a1 = b1 = 6;
            }
        }
    }
    The difference is that the initialisation of a1 and b1 occurs within a constructor. You cannot try and assign the result of evaluating b1=6 to a1 except in a method, constructor or initialisation block. Of these a constructor is the most obvious place.

    I guess the main point is that foo=bar=42 does not mean "assign 42 to foo and bar" as you might have supposed. It means something like "evaluate bar=42 and assign the result to foo".

    -----

    When you post code don't include the line numbers. And use the "code" tags: put [code] at the start of the code and [/code] at the end. Your code will also be more readable if you follow java coding standards and use capital letters to begin classes - like TstInner.
    Got it now, thanks for the help, much appreciated..

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: error Identifier expected?

    You're welcome.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default Re: error Identifier expected?

    Quote Originally Posted by pbrockway2 View Post
    I guess the main point is that foo=bar=42 does not mean "assign 42 to foo and bar" as you might have supposed. It means something like "evaluate bar=42 and assign the result to foo".
    Huh? Did they change the language overnight? As far as I know it does mean assign 42 to both variables.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    chandresh is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default Re: error Identifier expected?

    Quote Originally Posted by JosAH View Post
    Huh? Did they change the language overnight? As far as I know it does mean assign 42 to both variables.

    kind regards,

    Jos
    lol... I am sure the world hasnt moved that fast to change the language that drastically fast.
    What was mentioned did surprise me but then I was sticking to my learning that it does mean foo & bar gets the value of 42.
    thanks clearing the cofusion...

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default Re: error Identifier expected?

    Quote Originally Posted by JosAH View Post
    Huh? Did they change the language overnight? As far as I know it does mean assign 42 to both variables.

    kind regards,

    Jos
    You could have anything on the right hand side, though.
    The expression bar = 42 is no different to any other expression that could legitimately occupy that space. Taking operator precedence foo is assigned the result of bar = 42 in much the way it would be assigned the result of bar * 42 below:
    foo = bar * 42;

    It's nitpicking, though, as the end result is the same.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: error Identifier expected?

    Quote Originally Posted by Tolls View Post
    You could have anything on the right hand side, though.
    The expression bar = 42 is no different to any other expression that could legitimately occupy that space. Taking operator precedence foo is assigned the result of bar = 42 in much the way it would be assigned the result of bar * 42 below:
    foo = bar * 42;

    It's nitpicking, though, as the end result is the same.
    So the compiler has no problem with an uninitialized int variable being used in a multiplication operation?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default Re: error Identifier expected?

    Nothing in my example implies bar is not initialised.
    After all, 'a' and 'b' are both initialised in the original post at declaration.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: error Identifier expected?

    Quote Originally Posted by Tolls View Post
    Nothing in my example implies bar is not initialised.
    After all, 'a' and 'b' are both initialised in the original post at declaration.
    My fault, I misinterpreted what you were trying to prove with your example.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: error Identifier expected?

    Did they change the language overnight?
    No, nothing's changed.

    Within a constructor or method "foo=bar=42" has the effect of assigning 42 to both variables: the same effect is achieved with "foo=42; bar=42;".

    The meaning of "foo=bar=42;" - and the meaning of "foo=<complex expression>;" generally - is that the result of the complex expression is assigned to foo. In the context of the original post there is no restriction on assigning a value to two variables, but there is a restriction on assigning the result of a complex expression to a variable. The compiler will discriminate between "foo=bar=42;" and "foo=42; bar=42;".

    I don't much care whether you call the difference one of meaning or not. But, either way, the compiler will discriminate between them: allowing one, and forbidding the other.

  15. #15
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: error Identifier expected?

    Quote Originally Posted by pbrockway2 View Post
    No, nothing's changed.

    Within a constructor or method "foo=bar=42" has the effect of assigning 42 to both variables: the same effect is achieved with "foo=42; bar=42;".

    The meaning of "foo=bar=42;" - and the meaning of "foo=<complex expression>;" generally - is that the result of the complex expression is assigned to foo. In the context of the original post there is no restriction on assigning a value to two variables, but there is a restriction on assigning the result of a complex expression to a variable. The compiler will discriminate between "foo=bar=42;" and "foo=42; bar=42;".

    I don't much care whether you call the difference one of meaning or not. But, either way, the compiler will discriminate between them: allowing one, and forbidding the other.
    So outside of a method or constructor, [foo = bar = 42;] is not permitted because [bar = 42] constitutes as a complex expression. [foo = bar = 42;] is permitted inside methods and constructors because complex expressions can be assigned to variables within a method or constructor.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: error Identifier expected?

    So outside of a method or constructor, [foo = bar = 42;] is not permitted because [bar = 42] constitutes as a complex expression.
    Yes, you should try it. It's a complex expression whose value is being assigned to the variable. (Reading what I've posted again I think I've erroneously suggested it's the complexity that matters. But it's the assignment. And you can't escape assigning with foo=bar=42.)

    Contrast the following where the problem is reduced to its simplest:

    Java Code:
    class Good {
          // initialization
        int foo = 42;
    }
    
    class Bad {
        int foo;
            // assignment
        foo = 42;
    }

  17. #17
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: error Identifier expected?

    What still bothers me about this is that an assignment statement can be evaluated. Does the compiler just take the value of the variable on the left of the assignment operator when evaluating an assignment expression? That seems to be what is happening.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: error Identifier expected?

    Yes, the value of the assignment expression is the value being assigned. It gets "passed" continually to the left, assigning as it goes.

    But you can't assign where the OP was trying to - you can only initialise.

  19. #19
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: error Identifier expected?

    Yes, the value of the assignment expression is the value being assigned. It gets "passed" continually to the left, assigning as it goes.

    But you can't assign where the OP was trying to - you can only initialise.

Similar Threads

  1. <identifier expected> error. please help!
    By christopherx in forum AWT / Swing
    Replies: 9
    Last Post: 10-02-2011, 10:23 PM
  2. identifier expected error
    By Java Learner in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 08:13 PM
  3. getting identifier expected error . help me !
    By victorkeath in forum New To Java
    Replies: 3
    Last Post: 11-07-2008, 05:49 PM
  4. Identifier expected error
    By vasu18 in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 05:49 PM
  5. Error: <identifier> expected
    By barney in forum AWT / Swing
    Replies: 2
    Last Post: 07-31-2007, 07:38 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •