diego
diego

Reputation: 167

static member being accessed by instance reference

I have a call to a function and the background of that call is yellow and it says "static member being accessed by instance reference," but it works perfectly without errors.

Should I have to solve that somehow or is it okay?

Here is a code sample:

class A {
    static int x = 2;
    ...
}

Instantiation is some other file:

A a = new A();
a.x;

Upvotes: 11

Views: 23860

Answers (1)

Alberto Trindade Tavares
Alberto Trindade Tavares

Reputation: 10366

This warning happens when you have something like this:

class A {
 static int x = 2;
}

...

A a = new A();
a.x; // accessing static member by instance

You should access the static member x via the class (or interface) instead:

A a = new A();
A.x;

Static members belong to the class, not to a particular instance.

Upvotes: 38

Related Questions