Hello,

I would like to declare a class atribute so I wrote something like this:

class Xpto{
    private static int value;
}

However, once is private, I cant access it like Xpto.value nor a.value ("a" is a Xpto instance)
Declaring it protected I can access it through class and instances. Is there a way I can access it JUST through Class?

Thanks in advance!

Private variables can only be accessed by methods in the class that the variable was declared. You cannot access private variables outside of their class.

so, if you do want to access it like that, declare it public.

private - only access it in methods of the same class. If you don't like this but want to keep it private, either use get and set methods or you're out of luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.