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

Asked

Viewed 628 times

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 the onCreate: "The value of the local variable mc is not used"

Variable mc outside the onCreate: "mc cannot be resolved to a variable"

How can I use the variable outside the onCreate?

  • Use as class member variable.

  • after public class TracoActivity extends Activity { place private int mc;. Then you can do mc = 15; in the onCreate, and access on DialogLog. Study OOP in Java.

  • @pepper_chico Thank you very much! I will study more about it :)

1 answer

3


Whenever you need a variable in a certain 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 mistaken use of it. It may not occur on time but in the future you can forget that it should not be used and you can even use one variable thinking it is another.

If you declare in an inner scope the variable is not available to another part of the code 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 by the whole class.

Unlike the method variable which is always local, i.e., no one can see it other than the method, a class variable can be visible only within the class or also outside the class. Almost always leaving a visible class variable directly outside of it is considered a break from abstraction that can be considered a principle of object orientation. It’s the same thing you did with some methods, you said they are private and can’t be visible outside the class, IE, it’s an implementation detail, gives you more maintenance freedom. Whenever possible any member should be deprived, that is to say as little visible as possible.

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

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

Your code would look 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

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.