• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

encapsulating static variables

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'd like to encapsulate, but the following code makes the compiler unhappy:

class Player {
private static int playerCount = 0;
private String name;
public Player(String n) {
name = n;
playerCount++;
}
}

public class PlayerTest {
public static void main(String[] args) {
System.out.println(Player.playerCount);
Player one = new Player("numba ONE!");
System.out.println(Player.playerCount);
}
}



taking private off of the static var "fixes" everything, but doesn't that mean the static can be updated by any old method outside the class? what am i missing here?
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

You need to think about what the access modifiers (like private) mean. Why are these a good idea, when might you want to use them? You should also check out the behaviour when it comes to inheritance.

Often, people talk about making class members private, to force access through methods. This serves 2 purposes - as the author of the access method(s), you can add rules, validation, etc., you can also protect the user of the method from the actual type you use to store the data in (this means you could change the member type without affecting callers. A godd example would be the Collections classes. Do you know (or care) if the underlying store is an array, or some other object?

So thats a good reason for things being private, but there are times when that wouldn't make sense - Math.PI will always be the same value so thers no point hiding it!

Lastly, if you dont want your member to change, make it final and intialise it before anyone else can.

hope this helps

Ramen
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[I]i'd like to encapsulate, but the following code makes the compiler unhappy:


taking private off of the static var "fixes" everything, but doesn't that mean the static can be updated by any old method outside the class? what am i missing here? [/i]

1) If you use "code" tags rather than "quote" tags, you can maintain the formatting of your code, thus making it easier to read.
2) If you make playerCount private, nobody outside of the Player class can see it, including the main() method of the PlayerTest class. So you have choices. You can change the access of the variable to default, you can use a "getter" method (getPlayerCount), or maybe call a method in the Player class called "printCount()" or something like that.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be a good idea to have a PlayerFactory, a singleton class that creates Player instances and keeps count of them.

That way, you can more vividly see the encapsulation process:
a. a player's attribute is encapsulated in the Player class
b. the player objects' creation and count is encapsulated in the Factory class.

cheers.
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


taking private off of the static var "fixes" everything, but doesn't that mean the static can be updated by any old method outside the class? what am i missing here?


You can verywell declare a static variable Private.
The problem here is you are trying to access a private variable (playerCount) in main methods(ie outside your class). You would have got same error even if you would have used an instance variable.

so the compile time error is cox of private access-specifier and not static modifier

 
Krep Lock
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the answers. what really spurred the post was that the "getter" method i tried wasn't working. i soon realised that needed to be a static method and all seems to be working as expected.
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic