Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Availability of a static variable for several instances of a class

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about the availability of a static variable for instances of a class.

From the OCA/OCP Practice Tests Book (Chapter 6, Question 9):

Question: A ________ variable is always available to all instances of the class.

Answer: C. The only variables always available to all instances of the class are those declared static; therefore, Option C is the correct answer.

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.

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?
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static is considered a non access modifier. There is a list of a few:
  • Final
  • Abstract
  • Static
  • Strictfp
  • Native
  • Synchronized
  • Transient

  • However public, private, protected and default are access modifiers.
    There are many combinations non access modifiers and access modifiers that can be used together including static and private.

    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?


    I suspect that the answer is it has "access to this static field but may not provide it outwards (because of following certain rules)".
    But, you could create a small program to prove this to be true or false. I suspect that the program would be less then 50 lines of code and then you would know for sure.

    Hopefully, during your exam preparations you are creating many small programs to help prove or disprove things like this. Doing this helps tremendously and then you will know for sure.
     
    Marshal
    Posts: 25669
    69
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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.



    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?

     
    Marshal
    Posts: 69740
    277
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Nurettin Armutcu wrote:. . . the object cannot provide this variable to the caller since it is declared private. . . .

    The question says

    available to all instances of the class.

    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
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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.



    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?
     
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you create an instance of that object, you won't be able to use the object to access a private variable within the object. Unless you provide a GET property to be read from the outside.

    Only the procedures within the object can access that variable, but since the variable is static, any procedure *within* any instance of the object can access that unique static variable, which is common to all instantiated objects.
     
    Paul Clapham
    Marshal
    Posts: 25669
    69
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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?



    Remember that your original post was about static variables. But the rule you're talking about has nothing to do with whether a variable is static or not. The rule here is that private members of a class are not accessible to code outside that class. And "members" include both variables and method, whether they are static or not.

    And how is it regulated? Writing a few lines of code will quickly show you that the compiler prevents that rule from being broken. Really, give it a try. You'll find that writing code examples can be helpful in understanding rules like that.
     
    Nurettin Armutcu
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok, I see. In this context, especially private static variables interested me. It means an instance of a class has "normally" always access to a static variable, even if it is private. A public getter method could be a proof of that. The visibility & access rights etc. are checked during compilation, that's clear. Thanks for your posts.
     
    Yes, my master! Here is the tiny ad you asked for:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic