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

Condense declarations

 
Mike Jenkins
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyway to condense the declarations below?




I was hoping for something like this but it doesnt work:
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34671
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
The single line version compiles when I try it. What error are you getting?

Note that some people (including myself) find the expanded version clearer.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That is legal code, however it only initializes the last variable. You may want this instead:
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or this:
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I believe that last one is legal in some other languages, but it's not legal in Java.
[ November 16, 2007: Message edited by: Jim Yingst ]
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, you're right of course. Just goes to show how long it's been since I fell out the habit of using these shortened initializations. There is something to be said for making them explicit.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't; streets, schools and people are not declared in Java this way, so the compiler fails because of three unresolved symbols.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic