Reference Types

Hello guys, when I run the following, I get errors. Please, can someone explain the reason why… Thanks a lot

import java.awt.*;

public class Main {

public static void main (String[] args) {
    Point point1 = new Point(x:1, y:1);
    Point point2 = point1;
    point1.x = 2;
    System.out.println(point2);
}

}

Point(x:1 , y:2) => wrong
defined wrong parameter for Point constructor

class Point {
    public Integer x;
    public Integer y;
    Point(Integer x, Integer y){
        this.x = x;
        this.y = y;
    }
}

class App {
    public static void main(String[] args) {
        Point point = new Point(1,2);
        Point point1 = point;
        System.out.println("x = "+point1.x+" | y = "+point1.y);
    }
}

to run the code above, go to this website Online Java Compiler

This error happens all the time because Mosh’s editor displays the parameter names. You do not type the literal “x:” or “y:” so this should be: new Point (1, 1);

His editor adds the names for clarity, but Mosh is not literally typing that out.

1 Like

I did as you said, that worked. Appreciate your support. Thank you so much!

1 Like

Thank you so much. I appreciate your help. After correction, that worked now. Thank you!