File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes private static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "private static" Watch "private static" New topic
Author

private static

Jiaqi Lin
Greenhorn

Joined: Jul 20, 2007
Posts: 2
hi..i read this somewhere:

Guidelines in determining the visibility of data members and methods:

Declare the class and instance variables private.
Declare the class and instance methods private if they are used only by the other methods in the same class.
Declare the class constants public if you want to make their values directly readable by the client programs. If the class constants are used for internal purposes only, then declare them private.

i have always had the impression class variables and class methods are static fields, so it puzzles me that we should try to have them as private. anyone care to explain?
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Originally posted by Jiaqi Lin:
hi..i read this somewhere:

Guidelines in determining the visibility of data members and methods:

Declare the class and instance variables private.
Declare the class and instance methods private if they are used only by the other methods in the same class.
Declare the class constants public if you want to make their values directly readable by the client programs. If the class constants are used for internal purposes only, then declare them private.

i have always had the impression class variables and class methods are static fields, so it puzzles me that we should try to have them as private. anyone care to explain?



They're static if you make them static.


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Originally posted by Jiaqi Lin:

i have always had the impression class variables and class methods are static fields, so it puzzles me that we should try to have them as private. anyone care to explain?


erm, static methods and variables belong to the class, and as such are avaiable to every instantce of a class.

if by class variables you mean the fields of the class, these are not static and should, in general always be private with getters and setters.

public private (along with protected and default access) allow you to control the visibility of a given variable static or otherwise.

Just because a variable is static dosnt mean it wouldnt be dangrous to allow public access.



If for some reason you need to keep a count of how many objects of a given class you have in your applciation (perhaps bad guys in a game), then you would probably use a static vaiable as in the above example, because you want to keep a track of teh number of objects, you dont want the static count to be amendable by an external class so you should make it public.

Really I cant really think of a good reason for any varaible static or otherwise to be public. By making the varaible public you are breaking encapsulation (i think).

I hope I am answering your question and not what I thought your question was.

G
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private static