Local Inner Classes


Object-Oriented Programming
What is OOP?

The course is part of this learning path

Start course
1h 27m

In this course, we will learn the concepts of microservice and spring framework with a focus on object-oriented programming.

Learning Objectives

  • Object-oriented programming in Java

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

Hello there, my friends. In the previous video, we learned about nested classes. There were two types of nested classes: static and non-static classes. The non-static classes are also known as inner classes. Inner classes in Java can also be examined under two headings. These are local classes and anonymous classes. In this lesson, we will talk about the concept of local class, but we will examine the use of anonymous classes after learning the concepts of interface and abstract. So, now let's continue with local inner classes. Local classes are classes that are defined in a block, which is a group of zero or more statements between balanced braces. You typically find local classes defined in the body of a method. Local inner classes are the inner classes that are defined inside a block. Generally this block is a method body. Sometimes this block can be a for loop or an if clause. Local inner classes are not a member of any enclosing classes.

They belong to the block they are defined within due to which local inter classes cannot have any access modifiers associated with them. However, they can be marked as final or abstract. This class has access to the fields of the class enclosing it. Local inner classes must be instantiated in the block they are defined in. So, let's just get clear. Here are the main things you should know about local classes. The scope of the local inner class is restricted to the block they are defined in. A local inner class cannot be instantiated from outside the block where it is created. Until JDK 7, the local inner class can access only the final local variable of the enclosing block. However, from JDK 8, it is possible to access the non-final local variable of the enclosing block in the local inner class. A local class has access to the members of its enclosing class. Local inner classes can extend an abstract class or implement an interface. We will learn about the extend keyword, abstract classes, and interfaces in the next lectures. I promise. Local classes are similar to inner classes because they cannot define or declare any static members.

Local classes are non-static because they have access to instance members of the enclosing block. Starting in Java SE 8, if you declare the local class in a method, it can access the method's parameters. You cannot declare an interface inside a block. Interfaces are inherently static. You cannot declare static initializers or member interfaces in a local class. A local class can have static members provided that they are constant variables. A constant variable is a variable of primitive type or type string that is declared final and initialized with a compile-time constant expression. A compile-time constant expression is typically a string or an arithmetic expression that can be evaluated at compile time. So, that was a lot of information, but let's move on to the Eclipse and get some practice. First, I will create a class inside the nested class package. So, I will right click on the nested class package and select the new class options. The class name can be LocalClassExample. I will check the checkbox for the main method and click 'Finish'. Now consider the following example for this lesson.

We'll enter a phone number into the console and it will check it. If the number is 10 digits, it will print the valid number message on the console. If the number is not 10 digits, it will print the invalid number message. This example will make use of the local class. Let's get started if you're ready. First, I will create a method outside the main method, Public static void. The name of this method can be validate phone number. And this method will take the phone number entered by the user as a parameter. So, I write string phoneNumber. We will create the local class in this method, but first I will create an integer value. int numberLength = 10; This will take a fixed value. Using this variable, we will check if the entered number has 10 digits. So, I will assign the 10 to this variable. Okay, now we can create the local class, class PhoneNumber{ First, I will create a string variable to hold the entered number i.e. PhoneNumber. String validNumber, and its value will be null at the beginning. Now I will create the constructor of this class. Public PhoneNumber. If you remember, constructors had to have the same name as the class they were in, okay? And this constructor will take one parameter.

So, I write string number. My purpose in creating this constructor is, after writing the necessary codes for this class, I want the phone number to be entered at the same time while creating an object from this class in the method. If the phone number is not entered, I do not want the object to be created. Therefore, certain phone number will be entered, thanks to this constructor. Otherwise, this class will not be used. Okay, now I will create an if else statement to check the length of this number. if(number.length() == numberLength) i.e. is equal to 10, I will pass the number to the valid number variable. validNumber = number; At this stage, I would also like to point out that the numberLength variable we use here is actually an element of the method surrounding the local class, and that's why we were able to access this element directly. Prior to Java 8, we could only access variables that were marked as final. We will learn how to use the final keyword in the following lectures, but for now it's useful to know that prior to Java 8, we could only access final variables, whereas after Java 8, we could access final or non-final variables.

Okay, now let's continue with the else block. If the number.length is not equal to the numberLength, i.e. is not equal to 10, I will pass the null value to the valid number. validNumber = null; Okay, now the constructor is ready. Also, we can create a different method inside the local class. For example, let's create a method that shows the number entered by the user. public void printNumber(). And in this method I will show the phone number to the user using the println method, System.out.println("You entered"); After the plus sign I write the phone number variable. As you can see, we directly access the phone number parameter. The phone number parameter is the parameter of the enclosing method of the local class. So, after Java 8, we can directly access the method parameters in the local class. This is an important trick and you'll thank me later. Okay, now the local class is ready. Now it's time to use the local class. First, I will try to call the PhoneNumber local class in the main method.

And as you can see, we cannot access it. Now, let's try to call in the local class example class. Again, we cannot access the PhoneNumber local class. So, we only can access the local classes from inside the block it is defined. Okay, so that was the other trick about local class. Now let's continue. In the validate phone number method, I will create an object from the local class. PhoneNumber myNumber = new PhoneNumber. First, I will call the printNumber method here, myNumber.printNumber(); Now I will create an if else statement again. I will check if the validNumber is null or not. If myNumber.validNumber == null, I will give a message to the user. The message can be, "This number is invalid, The phone number must be 10 characters" else, the number is not equal to null. This time I will give this message to the user, "This number is valid" and I will print the valid number. After the plus sign, I write myNumber.validNumber.

Okay, the local class and the method that the local class is in are ready. Now we can continue with the main method. First, I will import the scanner class to get the number from the user. Scanner input = new Scanner(System.in); and I will give a message to the user to enter a phone number. System.out.println("Please enter a phone number: "); Now let's pass the phone number entered by the user to the string variable. String userNumber = input.nextLine. And now let's call the validate phone number, and I will pass the userNumber variable as the parameter of this method. Okay, now everything is ready. Let's run the code and test it. First, I will enter a five digit number. As you can see, first it printed the number I entered and then printed the invalid message. Let's run it again. This time I will enter a 10-digit number. And as you can see, this time the valid message is printed on the console. Okay, so that's how the local class is used. I hope you enjoyed. Let's take a quick break here, and I'll see you in the following video.


About the Author
Learning Paths

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.