Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


i would like to set my variables at the top of my class instead of in the method


i would like to set my variables at the top of my class instead of in the method  using -'java,android'

I can't seem to tackle this confusing problem, I have lots and lots of things that I would like to add at the top of my class to help cut down on clutter.

Since multiple methods use these checkbox variables.

I would like to have everything at the top directly under the opening bracket.

Here's what works, but not what I want.:

public class MyClass extends Activity implements View.OnClickListener {

    //leaving out most code like onCreate. Just pretend it's there.

    public void checkboth(View view) {

        CheckBox cb1 = (CheckBox) findViewById(R.id.cb1);
        CheckBox cb2 = (CheckBox) findViewById(R.id.cb2);

            cb1.setchecked(true);
            cb2.setchecked(true);

    }

    @Override
    public void onClick(View v) {
    }
}


But for the life of me I can't figure out why I can't do this:

public class MyClass extends Activity implements View.OnClickListener {

CheckBox cb1 = (CheckBox) findViewById(R.id.cb1);
CheckBox cb2 = (CheckBox) findViewById(R.id.cb2);

    //leaving out most code like onCreate. Just pretend it's there.

    public void checkboth(View view) {            

        cb1.setchecked(true);
        cb2.setchecked(true);

    }

    @Override
    public void onClick(View v) {
    }
}

    

asked Sep 29, 2015 by akhilesh
0 votes
7 views



Related Hot Questions



Walkin Jobs Opening



Government Jobs Opening


...