Java Inner Class

Java inward class is characterized inside the body of another class. Java internal class can be announced private, open, ensured, or with default get to though an external class can have just open or default get to.

Java Nested classes are separated into two types.

  1. static settled class

    If the settled class is static, at that point it's known as a static settled class. Static settled classes can get to just static individuals from the external class. A static settled class is equivalent to some other top-level class and is settled for just bundling convenience.

    A static class item can be made with the accompanying statement.

    OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

    java inner class

    Any non-static settled class is known as inward class in java. Java inward class is related with the object of the class and they can get to every one of the factors and techniques for the external class.

    Since internal classes are related with the occasion, we can't have any static factors in them.

    The object of java inner class are part of the outer class object and to create an instance of the inner class, we first need to create an instance of outer class.

    Java inner class can be instantiated like this;

    
    OuterClass outerObject = new OuterClass();
    OuterClass.InnerClass innerObject = outerObject.new InnerClass();
    

There are two special kinds of Java inner classes.

  1. local inner class

    If a class is defined in a method body, it’s known as local inner class.

    Since the local inner class is not associated with Object, we can’t use private, public or protected access modifiers with it. The only allowed modifiers are abstract or final.

    A local inner class can access all the members of the enclosing class and local final variables in the scope it’s defined. Additionally, it can also access a non-final local variable of the method in which it is defined, but it cannot modify them. So if you try to print non-final local variable’s value it will be allowed but if you try to change its value from inside method local inner class, you will get compile time Error.

    Local inner class can be defined as

    package com.journaldev.innerclasses; public class MainClass { private String s_main_class; public void print() { String s_print_method = ""; // local inner class inside the method class Logger { // able to access enclosing class variables String name = s_main_class; // able to access non-final method variables String name1 = s_print_method; public void foo() { String name1 = s_print_method; // Below code will throw compile time error: // Local variable s_print_method defined in an enclosing scope must be final or effectively final // s_print_method= ":"; } } // instantiate local inner class in the method to use Logger logger = new Logger(); } } We can characterize a neighborhood inward class inside any square as well, for example, static square, if-else square and so forth. Notwithstanding, for this situation, the extent of the class will be restricted. public class MainClass { static { class Foo { } Foo f = new Foo(); } public void bar() { if(1 < 2) { class Test { } Test t1 = new Test(); } // Below will throw error because of the scope of the class //Test t = new Test(); //Foo f = new Foo(); } }

    anonymous internal class

    A nearby inward class without name is known as unknown internal class. A mysterious class is characterized and instantiated in a solitary statement.

    Anonymous internal class dependably broaden a class or actualize an interface. Since an unknown class has no name, it is preposterous to expect to characterize a constructor for a mysterious class.

    Anonymous inward classes are available just at the point where it is defined.
    It's somewhat difficult to characterize how to make a mysterious inward class, we will see it's constant use in the test program below.

Here is a java class appearing at characterize java internal class, static settled class, neighborhood internal class, and an unknown internal class.

package com.journaldev.nested; import java.io.File; import java.io.FilenameFilter; public class OuterClass { private static String name = "OuterClass"; private int i; protected int j; int k; public int l; //OuterClass constructor public OuterClass(int i, int j, int k, int l) { this.i = i; this.j = j; this.k = k; this.l = l; } public int getI() { return this.i; } //static nested class, can access OuterClass static variables/methods static class StaticNestedClass { private int a; protected int b; int c; public int d; public int getA() { return this.a; } public String getName() { return name; } } //inner class, non-static and can access all the variables/methods of the outer class class InnerClass { private int w; protected int x; int y; public int z; public int getW() { return this.w; } public void setValues() { this.w = i; this.x = j; this.y = k; this.z = l; } @Override public String toString() { return "w=" + w + ":x=" + x + ":y=" + y + ":z=" + z; } public String getName() { return name; } } //local inner class public void print(String initial) { //local inner class inside the method class Logger { String name; public Logger(String name) { this.name = name; } public void log(String str) { System.out.println(this.name + ": " + str); } } Logger logger = new Logger(initial); logger.log(name); logger.log("" + this.i); logger.log("" + this.j); logger.log("" + this.k); logger.log("" + this.l); } //anonymous inner class public String[] getFilesInDir(String dir, final String ext) { File file = new File(dir); //anonymous inner class implementing FilenameFilter interface String[] filesList = file.list(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(ext); } }); return filesList; } }





© Journaldev Python 3 tutorial spring tutorial