Question

I am trying to access a private variable (x) in my method distanceFromPoint but it seems it doesn't work. How can I access it? My method is returning 0.0 all the time regardless of other values.

Code

public class Pointdeclare {
    private static int x;
    private static int y;

    Pointdeclare (int x_ , int y_ ){
        this.x = x_;
        this.y = y_;
    }

    int getX(){
        return x;
    }

    int getY(){
        return y;
    }

    static double distanceFromZero (){
        return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
    }

    double distanceFromPoint(Pointdeclare point){
        int distX = point.getX()- this.x;
        int distY = point.getY()- this.y;

        return (double) Math.sqrt(Math.pow(distX, 2) + Math.pow(distY, 2));
    }
}

Main Class

public class main {
    static Pointdeclare p1 = new Pointdeclare(6, 7);
    static Pointdeclare p2 = new Pointdeclare(3, 7);

    public static void main (String[] args){
        System.out.println(p2.distanceFromZero());

        System.out.print(p1.distanceFromPoint(p2));
    }
}
Was it helpful?

Solution 2

You shouldn't declare your class fields as static, just leave them private.

Btw consider using Point or Point2D native classes from java.awt.geom package.

OTHER TIPS

This will work better for you:

package cruft;

/**
 * Point2 description here
 * @author Michael
 * @link http://stackoverflow.com/questions/14087002/how-to-access-private-variables-in-a-java-class-method
 * @since 12/29/12 6:52 PM
 */
public class Point2 {

    private int x;
    private int y;

    Point2(int x_, int y_) {
        this.x = x_;
        this.y = y_;
    }

    int getX() {
        return x;
    }

    int getY() {
        return y;
    }

    double distanceFromZero() {
        return distanceFromPoint(new Point2(0, 0));
    }

    double distanceFromPoint(Point2 point) {
        int distX = point.getX()-this.x;
        int distY = point.getY()-this.y;

        return (double) Math.sqrt(Math.pow(distX, 2)+Math.pow(distY, 2));
    }


    public static void main(String[] args) {
        Point2 p1 = new Point2(6, 7);
        Point2 p2 = new Point2(3, 7);
        System.out.println(p2.distanceFromZero());
        System.out.print(p1.distanceFromPoint(p2));
    }
}

You declared "x" and "y" static, which means that they're class variables, not instance variables.

Because of that, every new call to the constructor will overwrite the old values. Thus distanceFromPoint always returns zero because there's only one x and one y.

The problem is not due to private, instead, it's because of static. x is static. In your method, you should use x as:

int distX = point.getX()- Pointdeclare.x; // You could use this.x because x is static.

I hope you find this relevant, it doesn't answer your question directly but I was recently informed my understanding of static was incorrect so having just researched the topic I thought maybe I could help. Static variables belong to a class and not its objects, objects of a class may access a static variable but no matter how many objects of the class there are there will only be one copy of the static variable. So I think what the people before me were saying is instead of p1 and p2 having their own copies of x and y both objects share the same x and y field therefore your value returned is 0. In other worlds your trying to find the distance between one location, it will always be zero. Hopefully that helps :-). I'm sorry I missed the first line of the main method. p2 should return a value as long as it isn't zero but p1 will not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top