Inner Class Java

INDICE TUTORIAL

Una classe interna (inner class) è una classe dichiarata all’interno di un’altra classe.

Esempio:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Esterna {
private int x;
public class Interna {
private int y;
public void metodoInterno(){
}
}
}
public class Esterna { private int x; public class Interna { private int y; public void metodoInterno(){ } } }
public class Esterna {
	private int x;
	
 	public class Interna {
 		private int y;
 		public void metodoInterno(){

 			
 		}
	 }
}

Per creare un’istanza della classe interna, se non dichiarata static, bisogna avere un’istanza della classe esterna.  La creazione può avvenire nei seguenti modi, tutti equivalenti:

Esempio

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Esterna a = new Esterna();
Esterna.Interna jk3 = a. new Interna();
Interna jk4 = a. new Interna();
Interna jk5 = new Esterna(). new Interna();
Esterna a = new Esterna(); Esterna.Interna jk3 = a. new Interna(); Interna jk4 = a. new Interna(); Interna jk5 = new Esterna(). new Interna();
			Esterna a = new Esterna();
			Esterna.Interna jk3 = a. new Interna();
			Interna jk4 = a. new Interna();
			Interna jk5 = new Esterna(). new Interna();

Una inner class ha accesso ai field ed ai metodi dell’ospite, tramite l’istruzione

         <nome_classe_contenitore>.this.

Esempio

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Esterna.this.x = 40;
Esterna.this.x = 40;
			Esterna.this.x = 40;

La classe inner può anche essere dichiarata statica. In questo caso, può essere considerata equivalente ad una classe normale che solo per motivi stilistici o di comodità è stata definita all’interno di un’altra classe.

Esempio

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Esterna {
private int x;
public static class InternaStatica {
private int y;
public void metodoInterno(){
//...
}
}
}
public class Esterna { private int x; public static class InternaStatica { private int y; public void metodoInterno(){ //... } } }
public class Esterna {
	private int x;
	
 	
 	public static class InternaStatica {
 		private int y;
 		public void metodoInterno(){
 			//...
 		}

	 }
}	

E’  possibile, inoltre, creare un’istanza della inner class statica senza creare la classe esterna in uno dei seguenti modi, tutti equivalenti:

Esempio

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Esterna.InternaStatica j = new InternaStatica();
InternaStatica r = new Esterna.InternaStatica();
Esterna.InternaStatica j = new InternaStatica(); InternaStatica r = new Esterna.InternaStatica();
		Esterna.InternaStatica j  = new InternaStatica();
		InternaStatica r = new Esterna.InternaStatica();

Esempio Completo

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Esterna {
private int x;
public class Interna {
private int y;
public void metodoInterno(){
Esterna.this.x = 40;
}
}
public static class InternaStatica {
private int y;
public void metodoInterno(){
//...
}
public static void metodoInternoDue(){
//...
}
}
public static void main(String[] args) {
Esterna a = new Esterna();
Interna l = new Esterna().new Interna();
InternaStatica r = new Esterna.InternaStatica();
r.metodoInterno();
Esterna.InternaStatica.metodoInternoDue();
}
}
public class Esterna { private int x; public class Interna { private int y; public void metodoInterno(){ Esterna.this.x = 40; } } public static class InternaStatica { private int y; public void metodoInterno(){ //... } public static void metodoInternoDue(){ //... } } public static void main(String[] args) { Esterna a = new Esterna(); Interna l = new Esterna().new Interna(); InternaStatica r = new Esterna.InternaStatica(); r.metodoInterno(); Esterna.InternaStatica.metodoInternoDue(); } }
public class Esterna {
	private int x;
	
 	public class Interna {
 		private int y;
 		public void metodoInterno(){
 			Esterna.this.x = 40;
 			
 		}
	 }

 	
 	public static class InternaStatica {
 		private int y;
 		public void metodoInterno(){
 			//...
 		}
 		public static void metodoInternoDue(){
 			//...
 		}
	 }
 	
 	public static void main(String[] args) {
 		
 			Esterna a = new Esterna();
 			
 			Interna l = new Esterna().new Interna();
 			InternaStatica r = new Esterna.InternaStatica();
 			
 			r.metodoInterno();
 			Esterna.InternaStatica.metodoInternoDue();
 		
 	}
}

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *