In this course, we'll learn the object-oriented concept in Java.
- Object-Oriented programming concept
- Object & Class
- Access Modifiers
- Naming Conventions
- Static Keyword
- Nested and Inner Classes
- 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 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 management 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 class. Also, we'll be able to define a static method in interface classes in Java 8 and later versions. The static variable is used for fulfilling the common requirement. For example, the school names of students. 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 class and interface in the next 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. Let's make a real life example by using static variables and methods. 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 of operations of a car by using static variables and methods such as showSpeed, speedUp, speedDown, and stop actions. 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 and 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'll use this method to print the current speed by using the println method. S out.println("Your current speed is") and after the plus sign, I write speed. We're going to call this method from the 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'll assign the sum of the values of the currentSpeed and the increase variables to the currentSpeed variable by using the add AND assignment operator. So, I write currentSpeed += increase; and I'll create an if-else statement here. If the current speed 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. Else if the current speed is smaller than the max speed then we show only the current speed by calling the showCurrentSpeed method.
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 currentSpeed 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'll 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, now the Car class is ready. We can test it but I'd like to mention one more thing before the testing. 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 define in this class are static. If I delete the static keyword in the currentSpeed variable, as you can see we are getting a compilation error because the methods we use, the current speed in are static but now the current speed is not static. In this case, we got a compilation error.
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. This was also an important point. Let's continue now. Let's create a new class for the test. I will right click on the staticexample package and select new and class. I'll 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 and 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. Second, 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. 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 max speed value i.e 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 stop the car and set the current speed to zero. 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. and 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 methods speedUp(int) from the type Car should be accessed in a static way. This code works but it is not recommended this way because we are doing something contrary to the purpose of using the static keyword. In short, it is valid but not recommended. Let's run the code. You see the same results. I hope you understood clearly what a static variable and method are. 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.