0

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();
}

...

2
  • read this docs.oracle.com/javase/tutorial/java/nutsandbolts/… Commented May 2, 2016 at 15:56
  • Those variables only exist within the scope of where they were defined, which was inside the spielStarten method. If you need access to those variables in other methods, then you either need to pass those variables to those methods, or you give those variables class level scope. Commented May 2, 2016 at 16:57

3 Answers 3

1

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();
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.