How to use variables in a location outside the scope where they were created?

2

I have the following code:

public class TracoActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_traco);

        int mc = 15;

    } //fecha onCreate


    public void ver_log(View v){
       DialogLog(); 
    }

    private void DialogLog(){
        final Dialog dialogLog = new Dialog(this);
        dialogLog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogLog.setContentView(R.layout.dialog_log);

        TextView relatorio = (TextView) dialogLog.findViewById(R.id.relatorio);
        final Button btnFechar = (Button) dialogLog.findViewById(R.id.btn_fechar);

        relatorio.setText("MC = "+mc);

        btnFechar.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dialogLog.dismiss();
            }
        });

        dialogLog.show();
    }

} //fecha class TracoActivity

Variable mc within onCreate : "The value of the local variable is not used"

Variable mc out of onCreate : "mc can not be resolved to a variable"

How can I use the variable outside the onCreate ?

    
asked by anonymous 26.12.2014 / 07:31

1 answer

3

Whenever you need a variable in a given scope you must declare it in that scope.

If you declare in an outer scope what you need it is accessible more than it should. The compiler will not give error but the code will allow a misuse of it. It may not occur on time but in the future it may be forgotten that it should not be used and may even use a variable thinking it is another.

If declared in an inner scope the variable is not available to another part of the code that you need. In this case, if you declare mc outside the method, that is, within the class the variable is available for the whole class, therefore accessible throughout the class.

Unlike the method variable that is always local, that is, no one can see it except the method, a class variable can be visible only within the class or outside the class. Almost always leaving a class variable visible directly outside it is considered a break in the encapsulation which is a principle of object orientation. It's the same thing you did with some methods, you said they are private and can not be visible outside the class, that is, it's an implementation detail, it gives you more freedom of maintenance. Whenever possible any member should be deprived, that is, the least visible.

You may be wondering if a variable should almost always be private how to access it externally to the class? Through so-called accessor methods. Have you seen methods declared as getCampo() and setCampo() ? Are they. Obviously they need to be public to give outside access. In general they are created in pairs but may have only one of them. Or you can have one public and the other private. You may have complex codes inside it but the most common is to have only return campo; and this.campo = parametro; respectively.

Did you notice this this ? You may already know how to use it and it may be optional in many cases but some programmers like to use it every time to ensure there is no ambiguity, not to confuse a class variable with a local variable or parameter. It may be exaggerated to always put but if putting becomes more explicit that the variable is class. In the background, this is a parameter that every method receives without you seeing it and it contains all members of the class.

Your code looks like this:

public class TracoActivity extends Activity {

    private int mc;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_traco);

        mc = 15;

    } //fecha onCreate

    public void ver_log(View v){
        DialogLog(); 
    }

    private void DialogLog(){
        final Dialog dialogLog = new Dialog(this);
        dialogLog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogLog.setContentView(R.layout.dialog_log);

        TextView relatorio = (TextView) dialogLog.findViewById(R.id.relatorio);
        final Button btnFechar = (Button) dialogLog.findViewById(R.id.btn_fechar);

        relatorio.setText("MC = " + mc);

        btnFechar.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dialogLog.dismiss();
            }
        });

        dialogLog.show();
    }

} //fecha class TracoActivity
    
26.12.2014 / 09:25