0

Which is the better way to define multiple TextViews?

private TextView textView1;
private TextView textView2;
private TextView textView3;

or

private TextView textView1, textView2, textView3;
6
  • 2
    You are just reducing lines, they both do the same thing in the compilers eyes. I actually even sometimes will do something like private String string1, string2, string 3; private String string4; if string1, string2, and string3 belong to a different section of the code than string4, but this is only a personal preference and I am sure it probably drives a lot of other people that read my code crazy :P Commented May 27, 2016 at 15:24
  • it dosen't matter, the result is the same for the compiler Commented May 27, 2016 at 15:25
  • Personal preference Commented May 27, 2016 at 15:27
  • stackoverflow.com/questions/20117500/… Commented May 27, 2016 at 15:28
  • Also I suggest you to use CamelCase naming convention for your variables. en.wikipedia.org/wiki/CamelCase Commented May 27, 2016 at 15:29

3 Answers 3

2

What u really asking is how the variables in java can be declared.

ideally the Java compiler will do an optimization on your first option

private TextView mytextview1;
private TextView mytextview2;
private TextView mytextview3;

reduce it to second option, so inside the compiler both your options lead to same result. Furthermore since most of the TextViews will not be assigned again the compiler will make those variables final as well. Check this for further clarifications final variable optimizations

Sign up to request clarification or add additional context in comments.

Comments

0

I would say use the second method because that would make the code mkre readable

 private TextView mytextview1;
 private TextView mytextview2;
 private TextView mytextview3;

And as you in whatever way you write it dosent matter you can write the whole code in one line also but that dosent look good..

Comments

0

When compiler make the bytecode for your code then it sends to VM before sending the bytecode to the vm the progaurd(If you use or activate progaurd in your project) does shrink and optimize your bytecode whatever and how you write source code in your project. So I think which code writing style make you and your team more understandable that should be followed. Your both coding style is valid and standard.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.