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

Declaring multiple objects on a single line

 
Amey Anekar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we declare more than one instances of a class on a single line??

like as we declare
int i1, i2, i3;

can we declare say for class.. foo

foo f1, f2, f3 = new foo();
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course we can:


But why didn't you just try it??
 
Manish Awasthy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
foo f1, f2, f3 = new foo();

if you do this only the last variable is initialized i.e. f3
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Btw it can be done in 2 lines:

Then all 3 references will reference to the same object.
 
Amey Anekar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:Btw it can be done in 2 lines:

Then all 3 references will reference to the same object.


Thank you...
You ppl are so nice
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic