In this course, we'll learn the object-oriented concept in Java.
Learning Objectives
- Object-Oriented programming concept
- Object & Class
- Access Modifiers
- Naming Conventions
- Constructors
- Packages
- Static Keyword
- Nested and Inner Classes
Intended Audience
- 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
Prerequisites
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there. In the previous video, we learned the nested class. There were two types of nested classes, static and non-static classes. The non-static classes is also known as inner class. Inner classes in Java can also be examined under two headings. These are local classes and anonymous classes. In this lesson, we'll talk about the concept of local class, but we will examine the use of anonymous classes after learning the concepts of interface and abstract. Okay, now let's continue with our 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 inner 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 class must be instantiated in the block they are defined in. 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 the outside block where it is created in. Till 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'll learn about the extend keyword, abstract classes and interfaces in the next lectures. 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. Now, let's move on to Eclipse and get some practice. First, I'll create a class inside the nestedclass package. So, I will right click on the nestedclass package and select the new class options. The class name can be LocalClassExample and I'll check the checkbox for the main method and click 'Finish'. Okay, now the example in this lesson will be like that. We'll enter a phone number into the console and it will check this number. If the number is 10 digits, it will print the valid number message but if the number is not equal to 10 digits, this time it will print the invalid number message on the console.
We'll make this example using the local class. If you're ready, let's start. First, I'll create a method outside the main method, public static void. The name of this method can be validatePhoneNUmber and this method will take the phone number entered by the user as a parameter. So, I write String phoneNUmber. We'll create the local class in this method but first, I'll create an integer value. int numberLength =. This will take a fixed value. Using this variable, we'll 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'll 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, a 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 the number.length() == numberLength i.e is equal to 10, I will pass the number to the validNumber 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. So, we were able to access this element directly. Before Java 8, we could only access variables that were final. We will learn the usage of the final keyword in the next lectures, but at this stage, it's useful to know that before Java 8, we can only access final variables while after Java 8, we can 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'll show the number to the user using the println method, "You entered". After the plus sign, I write the phoneNUmber variable. As you can see, we directly access the phoneNUmber parameter. The phoneNUmber 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. 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 LocalClassExample class. Again, we cannot access the phoneNumber local class. So, we can only access the local classes from inside the block, it is defined. Okay, this was the other trick about local class.
Now let's continue. In the validatePhoneNUmber method, I'll create an object from the local class. PhoneNumber myNumber = new PhoneNumber(). First, I'll call the printNumber method here. myNumber.printNumber(). Now, I will create an if-else statement again. In this if-else, I will check if the valid number 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'll 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'll import the scanner class to get the number from the user. Scanner input = new Scanner(System.in); and I'll 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 validatePhoneNumber 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 5-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, the usage of local class is like that. Let's take a short break here. 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.