Adam Novakovi
Adam Novakovi

Reputation: 819

Static variable fields not visible in debbuger

Hi I've got a very simple class defined like this

public class Pokus {


    public static String loginToken;
    public String neco = "neco";

    public Pokus() {
    }

    public static String getLoginToken() {
        return loginToken;
    }

    public static void setLoginToken(String loginToken) {
        Pokus.loginToken = loginToken;
    }
}

When I create an instance of this class

Pokus pokus = new Pokus();
pokus.setLoginToken("bla1212");

In a debugger I can see that object pokus has a field/variable called "neco" but not that static variable "loginToken".

debbuger in Android Studio

Is there any way to see static variables as well as the non-static ones?

Upvotes: 12

Views: 10908

Answers (4)

Vlad L
Vlad L

Reputation: 1694

You can right-click in the debugger's Variables area and select Customize Data Views...

In there, you can choose to add static and final static fields.

Upvotes: 2

Adam Novakovi
Adam Novakovi

Reputation: 819

Thanks guys I knew all of this but didn't know that debugger is taking this into consideration. There is an option to show static field Settings > Build,Execution, Deployement > Debugger > Data Views > Java

Upvotes: 24

Maciej Sikora
Maciej Sikora

Reputation: 20162

Debugger shows it properly, pokus is instance of class Pokus so it have standard method and properties from class Pokus, static methods and properties are in Class not in instance of Class. Static properties are shared for every object created from class Pokus ( or for every component in program if their are public ) so debugger properly not shows them as properties of single instance.

To show static variable examine class not instance. When debugger stops on breakpoint You can use console and write Pokus.someStaticVar and You will see current value. Console is available in debugger - https://i.sstatic.net/dMEhE.jpg.

Upvotes: 4

Shivam Jari
Shivam Jari

Reputation: 99

Static variables have the same values for all the instance of the class.Moreover they should be accessed using class and not an instance of the class. In Java, when you declare something as static, you are saying that it is a member of the class, not the object (hence why there is only one). Therefore it doesn't make sense to access it on the object, because that particular data member is associated with the class. That is the reason i think debugger does not show and it is correct behaviour

Upvotes: 0

Related Questions