Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why the 'identifier expected' error here?

 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the compiler say

collx.java:4: <identifier> expected
t.add(new Thing("Beaker"));
^

for this code?

 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List<Thing> t = new ArrayList<Thing>();
t.add(new Thing("Beaker"));

I don't know what do you mean by <Thing> in the above statements.

But, the code should be as shown below,

List t = new ArrayList();
t.add(new Thing("Beaker"));

Thanks,
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64972
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shiaber Shaam:
I don't know what do you mean by <Thing> in the above statements.
It's the use of Java 1.5 generics and it's perfectly legal.

But, the code should be as shown below,
No, the code statements are fine.

The problem is that these statements do not appear within a method or initializer block. That's not valid. The code needs to appear inside a code container.
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! I miss my coffee. I had to cut out all caffeine and I've been more or less brain-damaged ever since.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64972
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without coffee I am a miserable pile of jelly quivering in my chair. I feel for you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic