Java coding style (Android), should I declare variables on a separate line?

Java coding style (Android), should I declare variables on a separate line? … here is a solution to the problem.

Java coding style (Android), should I declare variables on a separate line?

I’ve been following the Intro to Android Apps MOOC on Udacity and learned a lot about good coding practices just by looking at the sample code in that class. However, today I found something puzzling :

// Third Step: Insert ContentValues into database and get a row ID back
    long locationRowId;
    locationRowId = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, testValues);

Why is locationRowId declared on a separate line? Changing the code above to

seems clearer

long locationRowId = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, testValues);

Is there a particular reason to include extra steps? Or I can safely use the single-line version.

Thanks

Solution

Yes, you can safely use the single-line version! As mentioned above, I think this is just your preference. There is no reason for or against this. But pick an option and use it later and don’t switch between them to get a clean code 😉

Related Problems and Solutions