Ravv
Ravv

Reputation: 482

What is best way to declaration a variable ? follow below example?

Example:

String one="One", two="Two", Three="Three";

(or)

String one="One";
String two="Two";
String Three="Three";

See, above e.g which one is best way in real time? what is difference both us? i am using 1st one it is save the class file memory. but all programmer use 2nd one.why there was use 2nd i did not understand?tell me any one

Upvotes: 1

Views: 188

Answers (3)

shazin
shazin

Reputation: 21883

Programming is always about maintainability. The code you wrote should be able to be modified by a developer coming in 100 years after you.

Maintainability rely heavily in code readability. Second one is more readable than the first one.

And further more second one is much more flexible. In the sense if you want to change the datatype of two in second method it is much easier than first method.

Upvotes: 8

Michal Borek
Michal Borek

Reputation: 4624

There is no difference. Compiler will do it in the best way, so in byte code you would have always (in both cases):

   // access flags 0x0
   Ljava/lang/String; one

   // access flags 0x0
   Ljava/lang/String; two

   // access flags 0x0
   Ljava/lang/String; Three

Going this way, use what is more appriopriate in your project. Don't care about the memory!

Upvotes: 5

Mena
Mena

Reputation: 48404

Both methodologies are equivalent, although the first one might be less readable under some circumstances. What you should not do is to declare a variable starting with an upper-case letter, as you do with your 3rd String.

Upvotes: 3

Related Questions