Lambda Expressions and Static Imports
The course is part of this learning path
In this lesson, we will learn the Lambda Expressions.
- Marker and Functional Interfaces
- Lambda Expressions
- Predicate Interface
- Static Import
- 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 lesson, we'll talk about the concept of Static Import. In our previous lessons, we talked about the concept of static and import. If you remember, we can access a variable or method defined a static directly using the class name. In other words, there was no need to create an object from the relevant class to use static properties. However, if we import static methods or variables using the concept of import and static, then we can use the relevant method or variable directly without using the class name. Let's look at the example on the slide for a better understanding. Let's say, we have a class called Car on the left, and this class can have a static method called showSpeed().
If we want to use the showSpeed() method in the main method, we can access the showSpeed() method directly using the class name. However, if we import the showSpeed() method using the import static keyword, we can now access the showSpeed() method directly without even needing the class name. Yes, I think the logic of static import is understood. Now let's move on to Eclipse and get some practice. First, I will create a new class. I right click on the 'lambdaexpressionexample' package and select the 'New', 'Class' options. The class name can be StaticImportExample, and I will check the check box for the main method and click the 'Finish' button. Yes, first of all, I'd like to give you examples from the Math class in the java.lang package because all methods in Math class are static.
If you're going to make an application about mathematical operations and you're going to use the Math class all the time, then you can use the static import concept for the Math class. Now let's do a few examples. For example, let's call the pow method we used before: Math.pow. This method takes two parameters and calculates the first number to the power of the second number. For example, let's write 2 and 3 here. This calculates two to the third power. Now, let's press the control key on the keyboard and click the pow method. As you can see, the pow method is a static method. Also, if we look at the other methods in the Math class, we can see that they are also static. Now let's continue where we left off. This time, let's call the max method, which is another method in the Math class. This method returns the larger of two numbers. I write 5 and 10 in parentheses.
As you can see, we can access these static methods directly using the class name. Now let's try not to use the class name with static import. I write import static java.lang.Math just below the package name. Then whichever method or variable I will use, I must write the name of that method or variable. For example, I'm typing pow, and let's copy and paste this on the bottom line. This will be max. Yes, we can now delete the Math class. As you can see, we didn't get any compilation errors. If you want, let's print these values to the console: sout pow. sout max. Now let's run the application and observe the result. As you can see, the application did not crash, and the result was successfully printed to the console.
Of course, if you notice, we've used separate imports for both pow and max methods here. However, you can automatically import all the methods in the Math class using the star, that is with the wild card. This is also valid. What you need to pay attention to here is that the static keyword must be absolutely necessary. If we remove it, we will get a compile error as you can see. Pay attention to this. In addition, out, which is a field of the print stream class which we use to print data to the console is also defined as static. So, if we also import the system class, we can no longer use system here. Let's do it now. import static java.lang.System.out. Now, let's delete the system classes here.
As you can see, we are not getting any errors. Let's run the application and test it. As you can see, the application is running successfully. Yes, in addition to these, we can also use static variables that we created ourselves in this way. Let's create a new class now. I right click on the 'lambdaexpressionexample' package and select the 'New', 'Class' options. This class name can be Car. The class will contain one variable and two methods. Let's create them. static int currentSpeed.
And the initial value of this variable will be 0. Now let's create the first method: static void showSpeed. This method will take one parameter: int speed. And this method will print the speed of the car. Sout "Your speed is " + speed. Let's create another static method. static void speedUp. This method will also take a parameter: int increase. Inside this method, I will increase the current speed according to the increased parameter. currentSpeed += increase. And I will print the current speed on the console using the showSpeed method. showSpeed currentSpeed. Okay, now let's try to access these properties from the main method. First, I will access them with the class name. And as you can see, we can access them with a class name. Now I will use the static import. import static lambdaexpressionexample.Car.*; Thus, we can use all static fields directly. First, I will call the showSpeed method and pass the currentSpeed variable as the parameter. And then, I'll call the speedUp method and pass the 100 as the parameter. Let's run and see. As you can see, the output was successfully printed on the console. Yes, I don't want to take it any further. As a result, you can access static fields directly with static imports. Let's take a short break here. See you in the next lesson.
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.