In Java, an inner class is a class that is defined inside another class. Inner classes are used to logically group classes and interfaces in a way that makes the code more readable and maintainable. Here's an example of how to define and use an inner class in Java:
```
public class OuterClass {
private int x = 10;
// Inner class
public class InnerClass {
public int getX() {
return x;
}
}
public static void main(String[] args) {
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner = outer.new InnerClass();
System.out.println(inner.getX()); // Output: 10
}
}
```
In This Example, `Outerclass` Has An Inner Class Called `Innerclass`. The `Innerclass` Has A Method Called `Getx()` That Returns The Value Of The Private Variable `X` From The Outer Class. In The `Main` Method, An Instance Of `Outerclass` Is Created And Then An Instance Of `Innerclass` Is Created Using The `New` Keyword With The `Outer` Object. Finally, The `Getx()` Method Of The `Inner` Object Is Called To Print The Value Of `X` To The Console.
Inner classes can also be declared as static. A static inner class is a class that is defined inside another class and is marked with the `static` keyword. Static inner classes are used to group classes and interfaces that are closely related to the outer class, but do not require access to the instance variables of the outer class.
Here's an example of a static inner class:
```
public class OuterClass {
private static int x = 10;
// Static inner class
public static class InnerClass {
public static int getX() {
return x;
}
}
public static void main(String[] args) {
System.out.println(OuterClass.InnerClass.getX()); // Output: 10
}
}
```
In This Example, `Outerclass` Has A Static Inner Class Called `Innerclass`. The `Innerclass` Has A Static Method Called `Getx()` That Returns The Value Of The Private Static Variable `X` From The Outer Class. In The `Main` Method, The `Getx()` Method Of The `Innerclass` Is Called Directly Using The Class Name, Without Creating An Instance Of The Outer Class.In Java, An Inner Class Is A Class That Is Defined Inside Another Class. Inner Classes Are Used To Logically Group Classes And Interfaces In A Way That Makes The Code More Readable And Maintainable. Here's An Example Of How To Define And Use An Inner Class In Java:
```
public class OuterClass {
private int x = 10;
// Inner class
public class InnerClass {
public int getX() {
return x;
}
}
public static void main(String[] args) {
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner = outer.new InnerClass();
System.out.println(inner.getX()); // Output: 10
}
}
```
In This Example, `Outerclass` Has An Inner Class Called `Innerclass`. The `Innerclass` Has A Method Called `Getx()` That Returns The Value Of The Private Variable `X` From The Outer Class. In The `Main` Method, An Instance Of `Outerclass` Is Created And Then An Instance Of `Innerclass` Is Created Using The `New` Keyword With The `Outer` Object. Finally, The `Getx()` Method Of The `Inner` Object Is Called To Print The Value Of `X` To The Console.
Inner Classes Can Also Be Declared As Static. A Static Inner Class Is A Class That Is Defined Inside Another Class And Is Marked With The `Static` Keyword. Static Inner Classes Are Used To Group Classes And Interfaces That Are Closely Related To The Outer Class, But Do Not Require Access To The Instance Variables Of The Outer Class.
Here's an example of a static inner class:
```
public class OuterClass {
private static int x = 10;
// Static inner class
public static class InnerClass {
public static int getX() {
return x;
}
}
public static void main(String[] args) {
System.out.println(OuterClass.InnerClass.getX()); // Output: 10
}
}
```
In this example, `OuterClass` has a static inner class called `InnerClass`. The `InnerClass` has a static method called `getX()` that returns the value of the private static variable `x` from the outer class. In the `main` method, the `getX()` method of the `InnerClass` is called directly using the class name, without creating an instance of the outer class.

No comments:
Post a Comment