aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Declaring multiple objects on a single line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Declaring multiple objects on a single line" Watch "Declaring multiple objects on a single line" New topic
Author

Declaring multiple objects on a single line

Amey Anekar
Greenhorn

Joined: Jan 02, 2010
Posts: 3
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

Joined: Oct 25, 2008
Posts: 2700

Of course we can:


But why didn't you just try it??


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Manish Awasthy
Greenhorn

Joined: Jan 11, 2010
Posts: 25
foo f1, f2, f3 = new foo();

if you do this only the last variable is initialized i.e. f3


SCJP 1.6 98%

"Successful People Don't Plan Results,They Just Plan Proper Beginings.Right Result Always Follow Right Begining".
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Btw it can be done in 2 lines:

Then all 3 references will reference to the same object.
Amey Anekar
Greenhorn

Joined: Jan 02, 2010
Posts: 3
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declaring multiple objects on a single line