29.01.2015 Views

Download dell'intero lavoro - Provincia di Torino

Download dell'intero lavoro - Provincia di Torino

Download dell'intero lavoro - Provincia di Torino

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

IL MODIFICATORE static<br />

ESEMPIO<br />

• Campi e meto<strong>di</strong> <strong>di</strong>chiarati static, sono associati alla classe e<br />

non a una particolare istanza<br />

class MyClass {<br />

static int a;<br />

…<br />

static void MyMethod() {<br />

a = a+1;<br />

}<br />

}<br />

• Pertanto: esiste una sola copia <strong>di</strong> un campo statico, con<strong>di</strong>viso da<br />

tutte le istanze della classe; non occorre istanziare un oggetto<br />

per usare un membro statico; meto<strong>di</strong> statici possono accedere<br />

solo a membri statici della classe<br />

• Sono qualificati con il nome della classe, e non della istanza<br />

MyClass.a =MyClass.a + 1;<br />

Introduzione al linguaggio Java 115<br />

class Safe {<br />

static int LastSerialNumber;<br />

Il costruttore Safe fa sì che<br />

public int SerialNumber;<br />

ogni istanza <strong>di</strong> Safe abbia un<br />

int SetCombination;<br />

SerialNumber unico<br />

….<br />

Safe(int Combination) {<br />

SerialNumber = LastSerialNumber++;<br />

SetCombination = Combination;<br />

}<br />

public static StartNewSeriesFrom (int Start) {<br />

LastSerialNumber = Start;<br />

}<br />

}<br />

….<br />

Safe.LastSerialNumber(100000); /* inizializza il numero <strong>di</strong> serie */<br />

Safe MySafe = new Safe(23456); /* crea una Safe <strong>di</strong> SerialNumber = 100001<br />

e combinazione 23456 */<br />

… MySafe.SerialNumber ….<br />

Introduzione al linguaggio Java 116<br />

VARIABILI LOCALI<br />

LA KEYWORD this<br />

• Le variabili locali a un metodo:<br />

– sono visibili solo dal corpo del metodo<br />

– vengono allocate (nello stack <strong>di</strong> run-time) alla chiamata e deallocate<br />

all’uscita del metodo<br />

– non vengono inizializzate automaticamente (<strong>di</strong>versamente dai<br />

campi <strong>di</strong> una classe)<br />

• Non si può accedere a una variabile a cui non si sia<br />

prima assegnato un valore (e viene segnalato in<br />

compilazione !)<br />

Esempio: int i;<br />

if ( cond ) { i = 55; … }<br />

i++; /* compile-time error */<br />

• Se una variabile locale ha lo stesso nome <strong>di</strong> un membro della<br />

sua classe, questo risulta invisibile (“shadowing”) …<br />

• … a meno <strong>di</strong> usare la keyword this, che denota l’oggetto<br />

corrente<br />

Esempio: class MyClass {<br />

int x, y, z; /* membri */<br />

void MyMethod (int x) { /* parametro */<br />

int y; /* variabile locale */<br />

y=x+1;<br />

/* y è la variabile locale,<br />

x è il parametro */<br />

z=this.x; /* this.x è il membro x */<br />

…<br />

}<br />

NB: Poichè this denota un oggetto, non si può usare con membri <strong>di</strong>chiarati static<br />

Introduzione al linguaggio Java 117<br />

Introduzione al linguaggio Java 118<br />

PASSAGGIO DI PARAMETRI<br />

A UN METODO<br />

COSTRUTTORI<br />

• Tutti i tipi primitivi sono passati per valore<br />

• Anche i riferimenti a oggetti <strong>di</strong> tipo reference sono<br />

passati per valore<br />

Es: int i;<br />

SomeObject obj = new SomeObject();<br />

i<br />

obj<br />

MyMethod( i, obj );<br />

….<br />

Void MyMethod (int j, SomeObject o) {<br />

j o<br />

j = j+1; /* non altera i, passato per valore */<br />

o = null; /* non altera obj, che si riferisce ancora<br />

all’oggetto */<br />

}<br />

heap<br />

• Sono meto<strong>di</strong> che vengono chiamati quando si crea<br />

una istanza <strong>di</strong> una classe; non ritornano alcun valore<br />

• Se non definisco alcun costruttore, per default viene<br />

fornito il costruttore vuoto:<br />

class MyClass {<br />

…<br />

MyClass() { }<br />

}<br />

• Possono essere overloaded; non ha senso che siano<br />

static<br />

Introduzione al linguaggio Java 119<br />

Introduzione al linguaggio Java 120<br />

Introduzione al linguaggio Java 20<br />

20

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!