Help with static variables in java

I have a class

class MyClass {
    private int val;
    public static final MyClass myObject = new MyClass(1);
    MyClass(int a){
        val = a;
    }
    public int getVal(){
        return val;
    }
    public MyClass func1(){
        MyClass temp = myObject;
        temp.val = 2;
        return temp;
    }
    public static void main(String [] args){
        MyClass x = new MyClass(4);
        System.out.println(myObject.getVal());
        x.func1();
        System.out.println(myObject.getVal());
    }
}

He prints:

 1
 2

I was expecting a print:

 1 
 1

It seems that on my part there is a fundamental misunderstanding. I expected that the value myObjectbeing the value final staticcannot be changed, and when I do MyClass temp = myObject, I create a new object with a temptype name MyClassand assign a value to myObjectthis newly created object. Please correct me if I am wrong. It seems that the new object is not created, but tempsimply points to the originalmyObject

EDIT: Thanks for the answers! Now I understand that the operator =never makes a copy of the object, it just copies the link. I need to make a copy myObjectand save it to temp. What would be the best way to achieve this?

EDIT2: Java?

class MyClass {
    private Integer val;
    public static final MyClass myObject = new MyClass(1);
    MyClass(int a){
        val = a;
    }
    public int getVal(){
        return val;
    }
    public MyClass func1(){
        MyClass temp = new MyClass(33);
        temp.val = myObject.val;
        temp.val = 2;
        return temp;
    }
    public static void main(String [] args){
        MyClass x = new MyClass(4);
        System.out.println(myObject.getVal());
        MyClass y = x.func1();
        System.out.println(x.getVal());
        System.out.println(y.getVal());
        System.out.println(myObject.getVal());
    }
}

 1
 4
 2
 1

, temp new MyClass(33), temp.val = 2, val. , temp.val myObject.val. ?

+3
7
MyClass temp = myObject;

, , temp , myObject.

, :

, temp of MyClass

, , .

, myObject func1(), (, , val, val ):

public MyClass func1(){
    MyClass temp = new MyClass(myObject.getVal());
    return temp;
}
+7

final, , . myObject var . func1() myObject, myObject.val 2.

+5

MyClass . ; myObj .

+2

, . , , MyClass temp = myObject; temp, myClass.

, .

public MyClass func1(){
    MyClass temp = myObject;
    System.out.println(myObject == temp);//print true
    temp.val = 2;
    return temp;
}
+1

Java = , , ++.

. , , , .

: ++ const, final , .

+1

, .

public static final MyClass myObject = new MyClass(1);

myObject - , val = 1. , . func1() 1 val 2. .

public MyClass func1(){
    MyClass temp = new MyClass(33);
    temp.val = myObject.val;
    temp.val = 2;
    return temp;
}
public static void main(String [] args){
    MyClass x = new MyClass(4);              //line A
    System.out.println(myObject.getVal());  //returns 1
    MyClass y = x.func1();                  //line B
    System.out.println(x.getVal());         //line C
    System.out.println(y.getVal());         //line D
    System.out.println(myObject.getVal());
}

In line A obj with val 4 is created.
Line B says func1, which ultimately creates a new object with val = 33, which then changes to val = 1 and then val = 2.
On line C, we get output 4 from the object created on line A.
On line D we we get result 2 from the changes we made to line B.
On line E, we get output 1 from a static destination that does not change.

0
source

Source: https://habr.com/ru/post/1752337/