The course is part of this learning path
In this course, we'll learn the object-oriented concept in Java.
- Object-Oriented programming concept
- Object & Class
- Access Modifiers
- Naming Conventions
- Static Keyword
- Nested and Inner Classes
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there, in this video we'll talk about the nested class related to the usage of the static keyword. In Java, we may define a class within another class. This class is known as the nested class. There are two types of nested classes in Java. The first one is the non-static nested class. This class is also known as the inner class. The second one is the static nested class. Non-static nested classes or inner classes may access all members such as static and non-static variables and methods of its outer class. Also an inner class may access private variables and methods of its outer class. However, static nested classes cannot access non-static methods or variables in the outer class, they can only access static variables and methods.
The access modifier is not important here. The situation does not change even if the properties have the public access modifier. Let's make some examples to understand better. In the object oriented programming project, right click on the source folder and select new and class. I will specify the package name as the nested class, and the class name as static nested example. Let's start to write a program that accesses static variables from nested class. Let's declare a static inter variable a, and its initial value can be five. Let's declare a private static inter variable b, and its initial value can be 10. Last, let's declare a non-static variable Int c, and its initial value can be 15. Let's try to access these variables from static nested class. We need to create a static nested class in this class. Let's create a static nested class named static nested class.
Static class, StaticNestedClass. In this class, I will create a void method named show, void show(). In this method, I will use three print methods, S.out.printInc ('' a=+a''). As you can see, in the first print method, we can access the static variable a. Let's create the second print method, S.out.printInc(''b= +b''). In the second print method, we can also access the private static variable b. And let's create the third print method. S.out.printInc( ''c=+c''). But as you can see, in the last print method, we cannot access non -private static variable c. This statement gives a compilation error because a static nested class cannot directly access to the non-static variables. So, let's put a comment on this line. Let's create a new class for the test of static nested class. I will right click on the nested class package and select new and class.
I'll specify the class name as static nested test and select the checkbox to add the main method. Now, I want to access the show method in the static nested class. Since this method is not static, to reach this method, we need to create an object from the inner static class. In order to access the inner static nested class, we first use the outer class and then we use the dot operator and nested class. You got it, first outer class, then inter class. So, I write, static nested example after the dot, I write StaticNestedClass. The outer class is static nested example and the inner class is static nested class. And if the show method in the inner static nested class was static, at this stage, we could add one more dot and access the static method directly. We explained this in detail in the previous lesson, but in this example since we do not define the show method as static, we must create a new object here and access the show method with this object. Now, let's create a nested object by using the new keyword nested = new staticnestedexample.staticnestedclass. Also, I'll show you another thing at this stage. If you don't use the outer class here like that you can remove, but this time you should import the static nested class. Yes, you heard right.
You can import it. If you remember we said that we perform the import process when using classes in different packages, but in this way, we can still use the import operation when working with nested classes. For example, all copy and paste this line and turn the previous line into a comment line because I want this usage pattern to stay here as well. Now let's delete the outer class from here and here. As you can see, we're getting a compilation error. If we move the mouse over the static nested class and select the import static nested class, as you can see, we have done the import process and we no longer receive errors from the compiler. Both usages are valid. Either import and do not use the outer class or create an object by writing the outer class and then the inner class before dealing with the import.
Yes, now we can call the show method using the nested object. The show method will print the values of variables a and b. Let's run the code. You see the values of variables a=5, and b=10. Now let's make an example with a non-static nested class i.e inner class. Let's create a new class. I will right click on the nested class package and select new and class. I'll specify the class name as inner class example. Let's copy the declaration of three variables from the static nested example class. Let's try to access these variables from inner class. We need to create an inner class in this class. Let's create the inner class named inner class. In this class, we need to create a void method show again. We use three print methods. In the first print method, we can access static variable a. In the second print method, we can access private static variable b. In the last print method, we can access non private static variable c. This is an important point.
In the static nested we couldn't access variable c, but we can access it in the non-static inner class. Shortly, if the inner class is static, other variables must be static to use them. Let's create a new class for the test of inner class. I will right click on the nested class package and select new class. I'll specify the class name as inner class test and select the checkbox to add the main method. In order to instantiate the inner class, we must first instantiate the outer class. Then create the inner object within the outer object. Let's create an outer object by using the new keyword first and then create an inner object. So I write InnerClassExample outer=new InnerClassExample. Now, let's create an object from the inner class, InnerClassExample.InnerClass. Inner = outer.new InnerClass. So, this syntax is a little bit different from the other. To create an object from the non-static inner class, you should write like that. Also, you can import the inner class again to not use the outer class here.
For example, let's delete this outer class here and I'll move the mouse over the inner class and select the import inner class option. As you can see, the compilation error disappeared, but you have to use this outer object here. If we remove this outer object here, we get a compilation error like that. No enclosing instance of type class example is accessible. Must qualify the allocation with an enclosing instance of type Inner class example. Example x.new a, where x is an instance of inner class example. As a result, to create an object from the Inner class, you should use this syntax. Now let's call the show method using the inner object. Now, let's run the code and see the result. You see the values of variables a b=10, and c=15. Yes, I think you understand the static and non-static inner classes. Let's take a short break here. I'll see you in the next video.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.