Java Nested and Inner Class
What are Classes?
In the Article, we will Discuss about the Nested and inner Classes of java.
In Java, a class is a blueprint or template for creating objects, which are instances of that class. A class defines the properties and behavior of the objects it creates.
Java Classes:
A class can have one or more variables, also known as fields or properties, that hold data values. It can also have methods, which are functions that perform operations on the data values or return data values.
Defining a Class in Java:
To declare a class in Java, you can use the following syntax:
Syntax:
[access modifier] class ClassName { // class members }
Explanation:
- [access modifier] is an optional access modifier that specifies the visibility of the class, which can be one of public, protected, private, or no access modifier (package-private).
- class is a keyword that is used to declare a new class.
- ClassName is the name you choose for the class, and it should start with a capital letter.
- The class members can include fields, constructors, methods, and nested classes.
In Java, there are two types of nested classes:
Syntax:
public class OuterClass { static class StaticNestedClass { //static nested class code } }
Java Example Program of Static Nested Classes:
public class OuterClass { // static nested class public static class StaticNestedClass { public void printMessage() { System.out.println("Hello from static nested class!"); } } public static void main(String[] args) { // create an instance of static nested class StaticNestedClass nestedObj = new StaticNestedClass(); nestedObj.printMessage(); } }
Output:
Hello from static nested class!
Explanation:
In this example, we have an OuterClass that contains a static nested class called StaticNestedClass. The nested class is declared as public static, which means it can be accessed from outside the OuterClass and does not require an instance of OuterClass to be created. The StaticNestedClass has a method called printMessage(), which prints out a message to the console.
In the main() method of OuterClass, we create an instance of StaticNestedClass using the syntax OuterClass.StaticNestedClass nestedObj = new OuterClass.StaticNestedClass();. We can then call the printMessage() method on this object.
Syntax:
public class OuterClass { private int x; class InnerClass { public void printX() { System.out.println(x); } } }
Java Example Program of Inner Classes:
public class OuterClass { // instance variable private int x = 10; // inner class public class InnerClass { public void printMessage() { System.out.println("Hello from inner class! x = " + x); } } public static void main(String[] args) { // create an instance of outer class OuterClass outerObj = new OuterClass(); // create an instance of inner class InnerClass innerObj = outerObj.new InnerClass(); // call inner class method innerObj.printMessage(); } }
Output:
Hello from inner class! x = 10
Explanation:
In this example, we have an OuterClass that contains an inner class called InnerClass. The inner class is declared as a non-static class inside the OuterClass.
The OuterClass has an instance variable x with a value of 10. The InnerClass has a method called printMessage(), which prints out a message to the console and accesses the x variable of the outer class.
In the main() method of OuterClass, we first create an instance of the outer class using the syntax OuterClass outerObj = new OuterClass();. We can then create an instance of the inner class using the syntax InnerClass innerObj = outerObj.new InnerClass();. Finally, we call the printMessage() method on the innerObj.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment