The course is part of this learning path
In this course, we will learn the concepts of microservice and spring framework with a focus on object-oriented programming.
- Object-oriented programming in Java
- 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 this lesson we will talk about the concept of static import. In our previous lessons we talked about the concepts of static and import. If you remember we can access a variable or a method to find 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 concepts 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 let this class 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.
Okay, so I think the logic of static import is understood. So, now let's move on to Eclipse and practice using status import. First, I will create a new class. In the object-oriented project, I right click on the source folder and select the new class option. The package name can be staticimportexample and the class name can be StaticImportExample. And I will check the checkbox for the main method and click the 'Finish' button. So, first of all, I would 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. So, let's look at some examples.
For example, let's call the power 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 two and three here. This calculates two to the third power. Now let's press the control key on the keyboard and click on 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 the 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. So, now we can 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. System.out.println(pow(2, 3)); System.out.println(max(5, 10)); 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 have 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 and totally works.
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 the 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. So, 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 staticimportexample package and select the new class options. The class name can be car. This class will contain one variable and two methods. Let's create them. static int currentSpeed, and the initial value of this variable will be zero.
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. System.out.println("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 increase parameter. currentSpeed += increase; And I will print the currentSpeed on the console using the showSpeed method. showSpeed(currentSpeed); Now let's try to access these properties from the main method. First, I will access them with the class name. As you can see, we can access them with the class name. Now I will use the static import. import static staticimportexample.Car.*; Thus, we can use all static fields directly. First, I will call the showSpeed method and pass the current speed variable as the parameter and then I will call the speedUp method and pass the 100 as the parameter. Let's run and see.
Yes, as you can see, the output was successfully printed on the console. So, I don't need to take this lesson any further. As you can see, you can access static fields directly with static imports. So, let's take a short break here and I'll see you in the next lesson, my friends.
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.