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.
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
Prerequisites
- Basic Java knowledge
Hello there. In this section, we'll explore the static keyword of the Java language in detail. We'll learn how we can apply the keyword static to variables, methods, blocks, and nested classes and what difference it makes. The static keyword is used for memory arrangement in Java. It's a keyword that is used for sharing the same variable or method of a given class. Usage of the static keyword. As you see in the slide, we use the static keyword to declare static variables, methods, blocks, and inner classes. We will also be able to add a static method to an interface class in Java 8 and later versions. The static variable is used to fulfill the common requirement. For example, the school names for students. The school name is common for all students. So, if we declare a static schoolName variable, it allocates memory only once in class at the time of class loading. The static variable can be accessed directly by the class name and doesn't need any object.
The static method is a method, which belongs to the class and not to the object. A static method can be accessed directly by the class name and doesn't need any object just like the static variable. The static block is a block of statements inside a Java class that will be executed when a class is first loaded into the Java Virtual Machine. We will cover nested classes and interfaces in the following slides. Let's examine the example in the slide to better understand the static keyword. If you remember, if we want to access the property of a class such as variable, method, etc., first, we should create an object of this class. And then using this object, we could access the properties of that class. But if we declare the properties of the class as static, this time, there is no need to create an object to use these properties. We can directly access the properties just by using the class name. In the example, we have a class called Add and inside this class, we have a non-static method called sum.
If we want to access this method inside the main method, we must first create an object from the Add class: myAddObject. Then we can access the sum method using this object. However, if we define the sum method as static, this time, we can access the sum method directly using the class name without creating an object in the main method. This is the main usage of the static keyword, okay? Let's use static variables and methods to create a real-world example. In the Object Oriented programming project, right-click on the 'Source' folder and select New Class. Specify the package name as staticexample and class name as Car. Let's start to write a program that implements the speed operations of a car by using static variables and methods such as showSpeed, speedUp, speedDown, and stopActions. First, let's declare two static integer variables. The first one is currentSpeed and its initial value is zero: public static int currentSpeed = 0; and the second is maxSpeed, its initial value is 180: public static int maxSpeed = 180. Let's declare a static void method named showCurrentSpeed: public static void showCurrentSpeed().
And it takes a parameter named speed. So, inside the parentheses, I write (int speed). We will use this method to print the current speed by using the println method, System.out.println("Your current speed is "+ speed); and after the plus sign I write speed. We're going to call this method from other methods in this class. We write a method once and we use it multiple times. We do not have to rewrite the entire code each time. Let's declare a static void method named speedUp: public static void speedUp(); and it takes a parameter named increase. So, inside the parentheses, I write (int increase). In this method, we will assign the sum of the values of the current speed and the increase variables to the currentSpeed variable by using the add AND assignment operator. So, I write currentSpeed += increase; and I will create an if-else statement here. If the currentSpeed of the car is greater than the max speed, then we show the current speed just by calling the showCurrentSpeed method here.
And as the parameter, I will write the currentSpeed variable. And also, we will give a ("Please slow down") message by using the println method. If the currentSpeed is smaller than the maxSpeed, then we only show the current speed by calling the showCurrentSpeed method. Okay, let's declare another static void method named speedDown: public static void speedDown(). And it takes a parameter named decrease. So, inside the parentheses, I write (int decrease). In this method, we will assign the difference of the values of the current speed and the increase variables to the currentSpeed variable by using the subtract AND assignment operator. So, I write currentSpeed -= decrease; Then, we show the current speed just by calling the showCurrentSpeed method here, and as the parameter, I will write the currentSpeed variable.
And lastly, let's declare the third static void method named stop. And this method will not take any parameter. In this method, we assign the zero to the currentSpeed variable and we show the current speed by calling the showCurrentSpeed method. Yes, the Car class is now ready. We can test it, but before we do, I'd like to point out something to you all. If you remember in our previous lessons, when we created a method other than the main method, we always created these methods as static because the main method is a static method and inside a static method, we can only call static variables or methods directly. Let me explain this through an example. Notice that all the variables and methods we defined in this class are static. If I delete the static keyword in the currentSpeed variable, now you can see we're getting an error, a compilation error. Because the methods we used for the currentSpeed were static but now the currentSpeed is not static. This time, we got a compilation error but to prevent this, you must either make the currentSpeed variable static again or create an object of this class, the Car class, where you will use the currentSpeed variable and you can access and use the currentSpeed variable by using this object. Got it?
Very important. But let's go on. Let's create a new class for the test. I will right-click on the 'Staticexample' package and select New and Class. I will specify the class name as Test and select the checkbox to add the main method. A static method can be accessed directly by the class name and doesn't need any object. So, let's call the methods of class by using class name directly. After I write Car, I put a dot. After putting a dot, we can see all the static variables and methods in the Car class. First, I want to display the initial value of the current speed on the console. For this, I will call the showCurrentSpeed method and I will assign the Car.currentSpeed as the parameter. Secondly, I will call the speedUp method and pass the value 60 as the parameter for increasing speed. And I will call the speedUp method again, and pass the value 160 as the parameter for increasing speed.
Then, I will call the speedDown method and pass the value 100 as the parameter for decreasing speed. Finally, we call the stop method. Okay, let's run the code. And let's review the results. So, the initial value of speed is zero, and we speed up to 60 and the current speed is 60. Then we increase the speed by 160 and it speeds up to 220, which is 60 + 160 and now the current speed is 220. 220 is greater than the maxSpeed value, for example, 180. So, we show the Please slow down message. Then, we decrease the speed by 100 and it speeds down to 120, which is 220 - 100, and now the current speed is 120. And finally, we stopped the car and set the current speed to 0. So, even though we use the static keyword, can we still create an object from the Car class and access methods or variables using this object? Let's try and see. If we try to create a Car object by using the new keyword for example, Car myCar = new Car(); and I write myCar., as you can see, we can also reach the variables and methods in the Car class but there is a little difference. So, let's call the speedUp method and pass the value 40 as the parameter.
As you can see, there is no error. But we get a warning message that the static method speedUp from the type Car should be accessed in a static way. This code works. But it's not recommended this way, because we're doing something contrary to the purpose of using the static keyword. In short, it does work and it is valid, but it's not recommended. Let's run the code. You see, the same results. I hope now my friends, you fully understand what a static variable and method are. So, let's take a brief break. Until the next video. Take care.
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.