Nurettin Armutcu wrote:My question at this point is: Does the object have access to this static field but may not provide it outwards (because of following certain rules) or does it have generally no access to the variable?
βThe strongest of all warriors are these two β Time and Patience.β β Leo Tolstoy, War and Peace
Nurettin Armutcu wrote:What exactly happens when the static variable is declared private? For example, we create an instance of the class (in which the private static variable is defined) outside the class or the package it is located in. In this case the object cannot provide this variable to the caller since it is declared private.
The question saysNurettin Armutcu wrote:. . . the object cannot provide this variable to the caller since it is declared private. . . .
That means you can have a field called field and an instance called object and you can write object.field. Or better, if it is static, you can simply write field. You need to try that and see whether the compiler will grant permission to write that code at all. Remember that being inside two instances of the same class counts as being inside the same class.available to all instances of the class.
I think you are imagining the situation where you create a reference to an instance of the class, and assign the reference to a variable which is in some other class. But then I can't imagine what you mean by "provide" and "caller" in that question. Perhaps you could provide an example of some code which illustrates your question?
That means you can have a field called field and an instance called object and you can write object.field. Or better, if it is static, you can simply write field. You need to try that and see whether the compiler will grant permission to write that code at all. Remember that being inside two instances of the same class counts as being inside the same class.
Nurettin Armutcu wrote:Ok, I understand your explanations but I try to explain my question more precisely. For example, if we have a class named Foobar with a private (static) variable and we create an object of this class outside the class, for example in a different package. We know that this Foobar object cannot provide us the variable, because the location where the invocation occurs, is outside the Foobar class. So we don't have access to the private variable. Is it so because the object has no access to the variable in this case or the object has access to it, but cannot provide it since the visibility rules do not permit this attempt? How is this regulated and from whom is this regulated, probably from the JVM?