先决条件: Java中的嵌套类
局部内部类是在 块 .通常,此块是一个方法体。有时,该块可以是for循环或if子句。局部内部类不是任何封闭类的成员。它们属于在其中定义的块,因此本地内部类不能有任何与之关联的访问修饰符。然而,它们可以被标记为最终的或抽象的。此类可以访问包含它的类的字段。本地内部类必须在定义它们的块中实例化。
本地内部阶级的规则:
- 局部内部类的范围仅限于 块 它们在中定义。
- 当地的内部阶级 不能 从外部实例化 块 它是在哪里创建的。
- 在JDK 7之前,局部内部类只能访问 封闭块 但是,从JDK 8中,可以访问局部内部类中封闭块的非最终局部变量。
- 本地类可以访问其 封闭类 .
- 本地内部类可以扩展抽象类或实现接口。
声明局部内部类 :可以在块内声明局部内部类。该块可以是方法体、初始化块、for循环,甚至可以是if语句。
访问成员: 本地内部类可以访问包含它的类的字段以及它在其中定义的块的字段。然而,这些类只有在声明为final或实际上是final时,才能访问包含它的块的变量或参数。一个变量的值在初始化后不会改变,它被称为有效的最终变量。在方法体中定义的局部内部类可以访问其参数。
编译时会发生什么?
当编译包含本地内部类的程序时,编译器会生成两个。类文件,一个用于外部类,另一个用于引用外部类的内部类。编译器将这两个文件命名为:
- 外面的班
- 外部1美元内部。班
方法体中的声明
JAVA
// Java program to illustrate // working of local inner classes public class Outer { private void getValue() { // Note that local variable(sum) must be final till JDK 7 // hence this code will work only in JDK 8 int sum = 20 ; // Local inner Class inside method class Inner { public int divisor; public int remainder; public Inner() { divisor = 4 ; remainder = sum%divisor; } private int getDivisor() { return divisor; } private int getRemainder() { return sum%divisor; } private int getQuotient() { System.out.println( "Inside inner class" ); return sum / divisor; } } Inner inner = new Inner(); System.out.println( "Divisor = " + inner.getDivisor()); System.out.println( "Remainder = " + inner.getRemainder()); System.out.println( "Quotient = " + inner.getQuotient()); } public static void main(String[] args) { Outer outer = new Outer(); outer.getValue(); } } |
Divisor = 4Remainder = 0Inside inner classQuotient = 5
注: 局部类可以访问封闭块的局部变量和参数 实际上是最终的 .
例如,如果在 内部的 类构造函数或任何 内部的 在上面的例子中:
public Inner(){ sum = 50; divisor = 4; remainder = sum%divisor;}
由于这个赋值语句,变量 总和 不是 实际上是最终的 不再因此,Java编译器会生成类似于“从内部类引用的局部变量必须是final或实际上是final”的错误消息
if语句中的声明
JAVA
// Java program to illustrate Declaration of // local inner classes inside an if statement public class Outer { public int data = 10 ; public int getData() { return data; } public static void main(String[] args) { Outer outer = new Outer(); if (outer.getData() < 20 ) { // Local inner class inside if clause class Inner { public int getValue() { System.out.println( "Inside Inner class" ); return outer.data; } } Inner inner = new Inner(); System.out.println(inner.getValue()); } else { System.out.println( "Inside Outer class" ); } } } |
Inside Inner class10
为内部类演示错误代码
JAVA
// Java code to demonstrate that inner // classes cannot be declared as static public class Outer { private int getValue( int data) { static class Inner { private int getData() { System.out.println( "Inside inner class" ); if (data < 10 ) { return 5 ; } else { return 15 ; } } } Inner inner = new Inner(); return inner.getData(); } public static void main(String[] args) { Outer outer = new Outer(); System.out.println(outer.getValue( 10 )); } } |
输出
Compilation error
说明: 上述程序导致编译错误,因为内部类不能声明为静态。内部类与它们在其中定义的块相关联,而不是与外部类(本例中为外部类)相关联。
JAVA
// Java code to demonstrate // the scope of inner class public class Outer { private void myMethod() { class Inner { private void innerMethod() { System.out.println( "Inside inner class" ); } } } public static void main(String[] args) { Outer outer = new Outer(); Inner inner = new Inner(); System.out.println(inner.innerMethod()); } } |
输出
prog.java:20: error: cannot find symbol Inner inner = new Inner(); ^ symbol: class Inner location: class Outer
说明: 上述程序导致编译错误,因为内部类的作用域仅限于它们在其中定义的块。
本文由 马扬克·库马尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。