brsven
brsven

Reputation: 17

Variables using between methods

Unfortunately I'm a total beginner in coding with java. My question now is why the variables runde, punkte in the starteRunde() method aren't defined. But actually I have defined them in the method above didn't I? Why can't I use these variables in the following methods?

public class GameActivity extends Activity implements View.OnClickListener{

    private void spielStarten(){
        boolean spielLaeuft = true;
        int runde = 0;
        int punkte;
        punkte = 0;
        starteRunde();
    }

    private void starteRunde(){
        runde = runde + 1;
        int muecken = runde * 10;
        int gefangeneMuecken = 0;
        int zeit = 60;
        bildschirmAktualisieren();
}

...

Upvotes: 0

Views: 49

Answers (3)

odpro
odpro

Reputation: 86

Because they are not global. to use this variables inside all methods, please, define they on the class level.

    public class GameActivity extends Activity implements View.OnClickListener{

        boolean spielLaeuft = true;
        int runde = 0;
        int punkte;

    private void spielStarten(){       
        punkte = 0;
        starteRunde();
    }

    private void starteRunde(){
        runde = runde + 1;
        int muecken = runde * 10;
        int gefangeneMuecken = 0;
        int zeit = 60;
        bildschirmAktualisieren();
}

Upvotes: 1

Ganesh Sagare
Ganesh Sagare

Reputation: 375

Java has 4 different kinds of variables
• Class variables
• Instance variables
• Local variables
• Parameter variables

Every variable has 2 properties:
• life time = the duration that a variable exists
• scope = the region in the program where the variable is accessible (can be used)

This Article explain you more details of variable scope in java

Upvotes: 0

Zircon
Zircon

Reputation: 4707

Variables have a scope, which is, simply put, the code block that they are defined in. Because you've defined those variables in a method, other methods can't access them because they are outside the scope of that method.

In this case, you might consider moving the variables to the class level:

public class GameActivity extends Activity implements View.OnClickListener{
boolean spielLaeuft;
int runde;
int punkte;
int muecken;
int gefangeneMuecken;
int zeit;

private void spielStarten(){
    spielLaeuft = true;
    runde = 0;
    punkte = 0;
    starteRunde();
}

private void starteRunde(){
    runde = runde + 1;
    muecken = runde * 10;
    gefangeneMuecken = 0;
    zeit = 60;
    bildschirmAktualisieren();
}
}

You also have the option of passing variables across methods as parameters, so, instead of defining the following variables at class-level, you could also code starteRunde like this if it works better for your next method call:

private void starteRunde(){
    int runde = runde + 1;
    int muecken = runde * 10;
    int gefangeneMuecken = 0;
    int zeit = 60;
    bildschirmAktualisieren(runde, meucken, gefangeneMuecken, zeit);
}

I recommend you do some research on variable scope so you can better understand it. The examples here do not fully explain the concept.

Upvotes: 0

Related Questions