Results 1 to 19 of 19
Thread: error Identifier expected?
- 08-15-2012, 02:11 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
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
- 08-15-2012, 03:04 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: error Identifier expected?
The following compiles OK:
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.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; } } }
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.
- 08-15-2012, 03:11 AM #3
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
- 08-15-2012, 03:13 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 08-15-2012, 03:19 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: error Identifier expected?
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:
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.Java Code:foo = 40 + 2;
Last edited by pbrockway2; 08-15-2012 at 03:21 AM.
- 08-15-2012, 09:36 AM #6
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
- 08-15-2012, 09:42 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: error Identifier expected?
You're welcome.
- 08-15-2012, 09:49 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 08-15-2012, 10:02 AM #9
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
Re: error Identifier expected?
- 08-15-2012, 10:08 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: error Identifier expected?
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.
- 08-15-2012, 03:17 PM #11
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
- 08-15-2012, 04:31 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 08-15-2012, 07:22 PM #13
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
- 08-15-2012, 11:15 PM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: error Identifier expected?
No, nothing's changed.Did they change the language overnight?
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.
- 08-15-2012, 11:27 PM #15
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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. [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
- 08-16-2012, 12:18 AM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: error Identifier expected?
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.)So outside of a method or constructor, [foo = bar = 42;] is not permitted because [bar = 42] constitutes as a complex expression.
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; }
- 08-16-2012, 01:59 AM #17
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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
- 08-16-2012, 05:43 AM #18
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 08-16-2012, 05:43 AM #19
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
<identifier expected> error. please help!
By christopherx in forum AWT / SwingReplies: 9Last Post: 10-02-2011, 10:23 PM -
identifier expected error
By Java Learner in forum New To JavaReplies: 1Last Post: 04-27-2010, 08:13 PM -
getting identifier expected error . help me !
By victorkeath in forum New To JavaReplies: 3Last Post: 11-07-2008, 05:49 PM -
Identifier expected error
By vasu18 in forum New To JavaReplies: 1Last Post: 01-01-2008, 05:49 PM -
Error: <identifier> expected
By barney in forum AWT / SwingReplies: 2Last Post: 07-31-2007, 07:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks