Originariamente inviato da Jamie04
Grazie, ora è più chiaro, anche se faccio fatica a memorizzare queste cose
Avrei bisogno di un chiarimento su questo passaggio:
To access a field, you can use a named reference to an object, as in the previous examples, or you can use any expression that returns an object reference. Recall that the new operator returns a reference to an object. So you could use the value returned from new to access a new object's fields:
int height = new Rectangle().height;
Non capisco cosa significa che l'operatore new ritorna la referenza a un oggetto. Esempio in questa istruzione:
Rectangle rect1=new Rectangle(10,15);
la referenza all'oggetto rectangle è "rect1", o no?
mentre qui:
int height = new Rectangle().height;
la referenza non è "height"?
E ancora:
This (int height = new Rectangle().height
statement creates a new Rectangle object and immediately gets its height. In essence, the statement calculates the default height of a Rectangle. Note that after this statement has been executed, the program no longer has a reference to the created Rectangle, because the program never stored the reference anywhere. The object is unreferenced, and its resources are free to be recycled by the Java Virtual Machine.
Anche qui non capisco...
Se l'oggetto creato con questa istruzione:
int height = new Rectangle().height;
viene "perso" dopo l'esecuzione dell'istruzione, a cosa potrebbe mai servire fare una cosa del genere?
Perdo l'oggetto rettangolo ma mi rimane una variabile intera height?
Forse se mi fate qualche esempio in cui queste istruzioni tornano utili mi rimane più comprensibile...
Grazie a tutti!