Java/Android which coding method is preferred?
<p>As a self taught coder I have a question about coding best practice. I have written the same program in two different ways, and I would like someone to give me the guidance on which way is preferred? This might be from a CPU overhead or RAM usage or simply a coding best practice perspective. I appreciate there may be many answers or theories to this question, and I know that isn't what Stack Overflow is here for, but to me this is a Stack Overflow question as my coding style is causing me some problems and I don't know which method I should be either sticking too or adopting.</p>

<p>Both examples have a simple XML layout file with two buttons and two updatable textviews (not included here).</p>

<p>Example 1. (the way I tend to code at the moment)</p>

<pre><code>public class MainActivity extends Activity {

    Button button1Add, button2Add;
    TextView text1Display, text2Display;
    int count1, count2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initilizeButtons();
        setupListeners();
    }

    public void initilizeButtons() {
        button1Add = (Button) findViewById(R.id.button1);
        button2Add = (Button) findViewById(R.id.button2);
        text1Display = (TextView) findViewById(R.id.textView1);
        text2Display = (TextView) findViewById(R.id.textView2); 
        count1 = Integer.parseInt(text1Display.getText().toString());
        count2 = Integer.parseInt(text2Display.getText().toString());
    }

    public void setupListeners() {
        button1Add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                count1++;
                text1Display.setText(String.valueOf(count1));
            }
        });

        button2Add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                count2++;
                text2Display.setText(String.valueOf(count2));
            }
        });
    }

}
</code></pre>

<p>Example 2. (the way I think I should be coding?)</p>

<pre><code>public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initilizeButtons();

    }

    public void initilizeButtons() {
        Button button1Add = (Button) findViewById(R.id.button1);
        Button button2Add = (Button) findViewById(R.id.button2);

        button1Add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                TextView text1Display = (TextView) findViewById(R.id.textView1);
                int headCount = Integer.parseInt(text1Display.getText().toString());
                headCount++;
                text1Display.setText(String.valueOf(headCount));
            }

        });

        button2Add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                TextView text2Display = (TextView) findViewById(R.id.textView2);
                int bodyCount = Integer.parseInt(text2Display.getText().toString());
                bodyCount++;
                text2Display.setText(String.valueOf(bodyCount));
            }

        });
    }

}
</code></pre>

<p>In example 1 I assume this will be quicker (i.e. less CPU) as I've declared my variables and just add to them throughout the programme. I'm worried here about RAM usage.</p>

<p>In example 2 I think this will cause more CPU usage as it has to redeclare all the variables each time they are used, but maybe it uses less RAM?</p>

<p>These are simply examples, and I'm sure they themselves will not cause much in the way of CPU or RAM overheads. I will apply the information from the answer you supply to my general coding practice.</p>
