Wpis z mikrobloga

pewnie chce pokazać coś dalej, np. że obiekt cutomer i anotherCustomer to te same obiekty i jak coś zmieni w anotherCustomer to zmieni się także w customer.


@zakopywywacz: A to nie jest tak, że w customer nic nie ulegnie zmianie? On zainicjował anotherCustomer obiektem customer, a potem dokonał zmiany pola anotherCustomer. To są dwa różne obiekty. Jeśli się mylę proszę o sprostowanie.
@fegwegw:

naprowadzę lekko: jak w Javie tworzymy obiekt?


Wywołując konstruktor danej klasy. No racja, anotherCustomer wskazuje na customer. To nie są różne obiekty. Sorki za zamieszanie. Pewnie autor chciał pokazać co to jest referencja.
@fegwegw: masz rację. To jest jeden obiekt. Nie zrobiliśmy niczego "new".

Customer customer = new Customer("KAMIL", 18.77);
Customer anotherCustomer; //creating another instance
System.out.println("Balance for customer " + customer.getBalance());
anotherCustomer = customer;
anotherCustomer.setBalance(12.1);

System.out.println("Balance for customer " + customer.getName() + " is " + customer.getBalance());
System.out.println("Balance for another customer " + customer.getName() + " is " + anotherCustomer.getBalance());

OUTPUT:

Balance for customer 18.77
Balance for customer KAMIL is 12.1
Balance for another