-2

I'm writing program which operates on coordinate system so I need to use coordinates pretty often. I decided to use Point class because it obviously allows to easily store coordinates of a point.

The problem is everywhere in my program I store it as int whereas Point returns double when using getX() and getY() methods. Of course I can easily cast it to int but it doesn't look very elegant and adds unnecessary mess to the code.

Is it ok if I will just get the value directly? Like this:

Point p = new Point(0, 0);
int x = p.x;

instead of:

int x = p.getX();

I'm not even sure if it makes any difference, I just know that getters and setters are there for a reason and should be used.

sajran
  • 317
  • 4
  • 15
  • https://stackoverflow.com/questions/11071407/advantage-of-set-and-get-methods-vs-public-variable – Mohamed Anees A May 27 '17 at 12:13
  • 3
    Possible duplicate of [Advantage of set and get methods vs public variable](https://stackoverflow.com/questions/11071407/advantage-of-set-and-get-methods-vs-public-variable) – Darshan Mehta May 27 '17 at 12:14
  • @MohamedAneesA I've seen this thread but it's about situation where I'm creating my own class. In my case class already exists and is not mine. I just want to know if it's bad practice to get the value directly. – sajran May 27 '17 at 12:17
  • I would suggest that if you wish to access the data in this way, you would be better served by creating your own class that contains `Point` and provides accessors delegating to the `Point`. In this way, you `Decorate` the class and leverage composition. If you then decide to store the data directly as `int` in the future, you would be protected. Edit: davidxxx has an example of just using your own directly. – KevinO May 27 '17 at 12:23

4 Answers4

3

I'm writing program which operates on coordinate system so I need to use coordinates pretty often. I decided to use Point class because it obviously allows to easily store coordinates of a point.

and

The problem is everywhere in my program I store it as int whereas Point returns double when using getX() and getY() methods.

Are you sure that it obviously allows to easily store coordinates of a point for your domain?
I don't think.
Using the java.awt.Point classes makes sense if you need to manipulate some java.awt.Point instances.

The class doesn't seem suit to your need.

  • If you really don't need to use java.awt.Point :

You should create and use your own Point class that returns int value when you write :

int x = p.getX();
  • If you change your mind and you happen to need to use java.awt.Point for some tasks:

In the custom Point class, add a method to return a java.awt.Point instance.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Great answer, thank you! I don't really know why but I thought it's much better to use existing class than create a very similar one. – sajran May 27 '17 at 12:32
  • You are welcome :) Interesting question. You are right : it's much better to use existing class than creating very similar one ... **but only if it addresses your need**. This class is rather old and is not very easy to use. It inherits from `Point2D` that manipulates `double`. It is not really a concern if `Point` had also specific methods : `getIntX()` and a `getIntY()`. But it has not them. So, using `Point` makes sense only when you **are compelled to** or **you can take advantage of it** (for example to send a Point object to an existing or a third party class that do a task for you). – davidxxx May 27 '17 at 12:46
1

@Sarjan Yes...some might argue that setters and getters are evil. But I guess it perfectly helps in various situations particularly in large projects. A sample example is validation. Say,if you want to set x only if it is greater than 0, you cannot perform it elegantly if it is public(unless you have a check before setting the variable..everywhere!!!...Tedious right?? And missing it will crash your system someday if some new programmer works on your project and unaware of this)..if you use a setter, you can check if it is greater than 0 inside the setter itself and thus provides easy implementation!!

PS: I couldn't comment this since it is too long and hence written as answer.

Mohamed Anees A
  • 4,119
  • 1
  • 22
  • 35
1

You have linked documentation about Point. So read carefully and think of your problem rather that starting to code and thinking, First think of an idea. Then get to know that the java.awt.point class is best fit to your situation or not.

If it is not fit to your situation you can implement a class. Otherwise, follow documentation. It is a good practice to follow documentation and also OOP concept.

Point p = new Point(0, 0);

If you have followed the documentation, you will see like this:

Point(): Constructs and initializes a point at the origin (0, 0) of the coordinate space.

I think you donot have clear idea about this. However,

Point p = new Point();
int x = p.x;

This is not a problem because x is public and its your program. But why they have implemented this:

getX();

Also read these- getters and setters, implementation of Point class.

Blasanka
  • 21,001
  • 12
  • 102
  • 104
0

Let, You have 2 classes. These are: Class A & Class B

In Class A, you have an private instance variable.

Class A:

Class A {
    private int a;

    public int getA() {
        return A;
    }

}

Now you want to access variable named a of Class A from Class B. But the variable is private. So, now we need the getter method as the getter method is public.

Class B:

Class B {
    public void fetchA() {
        A classA = new A();
        int a = classA.getA();
    }
}

In this case, we need getter methods.

Avijit Karmakar
  • 8,890
  • 6
  • 44
  • 59