[java] Class Variables

Started by
12 comments, last by Wrathnut 23 years, 7 months ago
To clarify that a little:

A singleton class is a class with a static getter which will return a single instance of a class, every class which calls that static method, will receive a reference to that one instance of a class.
The purpose of a singleton class, is to prevent the use of Global variables.
For example, Runtime.getRuntime() in Java, is an example of a singleton class.

Singleton classes and other similar concepts, are detailed in a book called "Design Patterns".

Happy Coding
"We who cut mere stones must always be envisioning cathedrals"
Advertisement
This is in regards to the public vs private class variables. I don't see the difference in making a variable public, or making it private and creating a get/set method. Both are bad in my opinion. As an example: (this example is in no way complete, just using it as an example)

this:
            class Sprite{   private int x;   private int y;   void setX(int x)   {      this.x = x;   }}is really no different from this:class Sprite{   public int x;   public int y;}            


The whole point behind encapsulation is to seperate the interface from the implementation. In the above examples if we wanted to all of a sudden change the coordinate system from int to long, we would have the same problem with all calling code. Either we search for every reference to Sprite.x, or we search for every reference to Sprite.setX.

Bret.

Edited by - Hitman on September 14, 2000 12:35:02 AM

Edited by - Hitman on September 14, 2000 12:35:33 AM
Well one point of using public setters is if we want to change the internal representation without changing any code.

lets say that x,y was realtive coordinates of some kind. If I changed them to be absolute coordinates the old code would break. I could now change setX and setY to take this into acount (by adding origin) and mae a setAbsX and setAbsY for setting the absolutes directly. This way code clients that otherwise would have been broken now still works.

Or lets say that x,y, suddenly where to be stored in a C++ program instead. Again setX and setY could be changed to calling native data while by using x and y directly all your code using x and y would have to be rewritten.

------------

TheEarl,
true, "Design patterns" use a getInstance() variable to retrieve the isntance, but by using a class variable you achieve them same thing - so whats the difference? It is still a singleton.

Jacob Marner
Jacob Marner, M.Sc.Console Programmer, Deadline Games
If you make your class variables private and provide set and get methods for them, these variables are now considered "properties" and you can use JavaBeans introspection to obtain the property names, and to get and set their values.

This makes it easier to implement a pluggable, extensible architecture.

Of course, as felonius said, hiding the internal representation of the properties is another real benefit.
I am a Jedi, like my father before me

This topic is closed to new replies.

Advertisement