Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes static variable doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static variable doubt" Watch "static variable doubt" New topic

static variable doubt

Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
Hi Friends,
I have a circle class that generates circles. The circle has a static count variable which is used in the constructor to count the no. of circles created. My question is that would it be a good idea to make the count variable private and provide a static method to access it.I would be obliged if someone could please clear this doubt.


SCJP 1.4 (88%)<br />SCWCD 1.4 (88%)
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

It's a good idea to make all member variables private. And only provide a getX() method if some code outside of the class truly needs to know that value. Seriously.

So the answer to all of your questions is "yes".

[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Also, it is a bad idea to access a static variable as "this.count" because it looks like a instance variable. Instead, you should use "Circle.count" to clarify that it is a static class variable.


Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: static variable doubt
It's not a secret anymore!