I am a C# developer and I am highly habituated to initialize a variable in the same declaration statement as
ClassA myClass = new ClassA();
To my point of view, this practice is concise, more readable and looks neat.
Now, I am learning java for android. So far whatever java snippet I am facing, everywhere I see that the snippet writer is using code like this:
ClassA myClass;
myClass = new ClassA();
Now, I understand that, my question may sound silly, but really curious to know, is there any impact / effect or is there any difference between these 2 approach ? I mean, if I compile a code in java like this :
ClassA myClass = new ClassA();
is there anything about it that matters internally ? I just want to be sure that I am not doing anything wrong.
No, this isn’t a C#/Java difference, and your habit is appropriate. There’s simply no good reason to split declaration and initialization unless you have to due to the initialization being conditional (
if/else).I’m sure there’s plenty of Android code which is written appropriately, just as there’s plenty of bad C# out there. It sounds like you’re just getting unlucky (or perhaps reading lots of code by the same author, who has an unfortunate style).