The course is part of this learning path
In this course, we'll learn about the Java Input/Output subject.
Learning Objectives
- API and Java I/O
- OutputStream Class
- InputStream Class
- Reader Class
- Writer Class
- Serialization, Deserialization, and Non-Serializable objects
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 our previous lesson, we learned the serialization process. We also mentioned that the serializable class should be implemented for serialization. Unfortunately, not every object in a class that we want to serialize can be serialized. For example, even if we create an object of a class that does not implement the serializable interface in a class that implements the serializable class, that object will not be serialized. In this case, the NotSerializableException is thrown. If we are faced with such a situation, this time, we can solve it by using the transient keyword. Also, if we apply the transient keyword to a serializable object, then the default value of that object is saved. For example, if we apply the word transient to a variable of serializable int data type, then the value of this variable is also saved as zero because the default value of the int data type is zero.
Yes, after this brief information, let's move onto Eclipse and examine it practically. We will continue on our previous project. First of all, I'll create a new class in the serialization example package. So, I right-click on this package and select the New Class options. The class name can be SpeedOfCars, and I'll click the 'Finish' button. Okay. In this class, I'll create a maxSpeed variable with int type: int maxSpeed. Also, I'll create the constructor of this, public SpeedOfCars (int maxSpeed). And in the constructor, I'll write this.maxSpeed = maxSpeed. Okay. Now, I will create an object from this class in the Car class.
So, I open the Car class. Here, I write SpeedOfCars carSpeed. And I will add one more constructor parameter: int speed. And in the constructor, I'll write an object from the SpeedOfCars and assign it to the carSpeed: carSpeed = new SpeedOfCars. And as the constructor parameter, I write speed. Okay, now I will create a property of this Car class to get this speed and show it with other properties. Here, I'll add the my speed property. And now in the constructor, I will assign the value of the maxSpeed variable of the SpeedOfCar class to the my speed. My speed = carSpeed.maxSpeed. Also, to show this data on the console, I will call it in the two string method. After the isAutomatic property, I write + "\nMaxSpeed: " + this.Speed.
Okay, and I'll save the code. Now, in the Serialization Test class, we need to assign the value of the speed parameter. The speed of the first car can be 350. The speed of the second car can be 300. The speed of the third car can be 250. Okay. Now, let's try to run the app. And as you can see, we get an exception. This is the NotSerializableException, because although the Car class implements the serializable interface, the car speed object is the object of the SpeedOfCars class, which does not implement the serializable interface. Therefore, while the objects of the Car class are serialized during the serialization phase, the objects of the SpeedOfCars class cannot be serialized. And, in this case, a NotSerializableException is thrown. I think you understand the non-serializability case. Now, let's try to overcome this error. It will be sufficient to use the transient keyword for this.
I use the keyword transient just before the carSpeed object in the Car class. Let's save the project and run it again. And as you can see, we did not encounter any errors this time. Now, let's run the DeSerialization Test class and observe the data in the console. And as you can see, the speed data for each car is also printed to the console this time. We also said that if we apply the transient keyword to a serializable object, the default value of that object will be saved. Let's test this now. I write the keyword transient right before the isAutomatic in the Car class. In this case, the isAutomatic values of all cars will be false because the default value of the Boolean data type was false. Let's save the project and test it again. Let's run the Serialization Test class first. The data is printed. Now, let's run the DeSerialization Test class. As you can see, the isAutomatic values of all cars are printed as false. Yes, that's the concept of non-serializable objects. I hope it's understood. Let's take a short break here. See you in our 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.