Hi everyone, I'm working on a summer coding project with few friends from school and we have a design issue that we need some input on.(The language is Java) We have for example a 2D vector class called Vector2. It has two properties float X, and float Y. When I was learning Java I was always told that these should be private and have getter and setter methods. The debate is now that if I want to say add up the x components of two vectors and set that to the x component of another vector we now have a bit of a mess. example
myNewVec.setX(vecA.getX() + vecB.getX());
If we had set the fields public we could have much cleaner syntax as follows:
myNewVec.x = vecA.x + vecB.x;
So aside from the nice syntax the other argument is why make the extra methods for set and get x,y if we are providing the same functionality as just setting it public anyways. Example
public float getX()
{
return this.x;
}
public void setX(float x)
{
this.x = x;
}
is the same as just using myVec.x = someOtherValue; or just returning return myVec.x
So basically TL;DR Looking for the input of someone with more experience then us to help us make a decision since we do not have the getters;setters; of C# nor the operator overloading of c++. Am I overlooking something important making it public or is it justified? Is having the "syntactic sugar" worth going against what we were taught? Thanks :)