Lazy Loading in Java8 May 2025 | 5 min read The concept of delaying the loading of an object until one needs it is known as lazy loading. In other words, it is the process of delaying the process of instantiating the class until required. Lazy loading is important in a scenario where the cost of creating objects is high, or usage of the object is rare in the program. Lazy loading is a technique that enhances the efficiency of the program. In this section, we will discuss lazy loading in detail. Implementations of Lazy LoadingVirtual ProxyVirtual proxy is the memory saving process that promotes the postponement of object creation. Observe the following program. FileName: LazyLoadingExample.java Output: Company Name: ABC Company Address: India Company Contact No.: +91-011-55512347 Requesting for the contact list ... Fetching the list of employees ... Employee Name: Mukesh, EmpDesignation : JSE, Employee Salary : 3452.67 Employee Name: Amit, EmpDesignation : ASE, Employee Salary : 22345.0 Employee Name: Naman, EmpDesignation : G4, Employee Salary : 3256.17 Employee Name: Vipul, EmpDesignation : SDE1, Employee Salary : 4895.34 Employee Name: Akhil, EmpDesignation : SDE2, Employee Salary : 2857.91 Explanation: In the code, we have instantiated the ContactListProxy class. At this point, the list of employees is not created. It is because the list of employees is not required at this point of time. When the list of employees is required, the method getEmployeeList() is invoked, and at the same time, the list of employees is created, which shows the creation of the list of employees is delayed until required. Lazy InitializationThe Lazy Initialization technique demonstrates the value check of a class field when its usage is required used. If that class field value is null, then the field gets updated with the proper value before it is returned. The following example illustrates the same. File Name: LazyLoadingExample1.java Output: The number of instances created = 1 Mercedes The number of instances created = 2 Audi Mercedes The number of instances created = 3 Audi BMW Mercedes Explanation: In the code, the getCarByTypeName() method does the lazy initialization of the map field. It first checks whether the asked car type is present or not. If not present, the concerned car type is instantiated and then loaded on the map. Note that the constructor of the class Cars is made private intentionally. The private constructor ensures that one cannot create an object of the class Cars at any time. An instance of the class Cars is only created when we invoke the method getCarByTypeName(), which shows that an appropriate instance of the class Cars is only created or loaded when required. |
Compare the structures and node values of two binary trees to check if they are mirrors. A binary tree is a mirror of another if the left subtree of one matches the right subtree of the other, and vice versa. This involves recursion to traverse and...
9 min read
In this section, we will understand what is saddle point in a matrix, and how to find the saddle point of a matrix through a Java program. What is the saddle point in a matrix? In a matrix, an element is called the saddle point that is the...
3 min read
? In programming, most of the time we have to deal with a string that is an important part of the programming language. Sometimes, we require to convert the whole paragraph into a sentence case. In such a case, the first letter of the string must be...
2 min read
In the world of Java programming, data processing is a common task that often involves manipulating collections of objects. Prior to the release of Java 8, performing operations on collections required writing verbose and error-prone code using loops or external libraries. However, with the introduction of...
5 min read
Java is a computing platform, class-based, high-level object-oriented programming language that has been first released in the year 1995 by Sun Microsystems. From humble beginnings, it has evolved to power a large share of today's digital world by giving a reliable platform using which many...
3 min read
? Java Timer Class In Java, Timer is a class that belong to the java.util package. It extends the Object class and implements the Serializable interface. The class provides the constructors and methods that can be used to perform time related activities. Using the Timer class, we can...
2 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, crit-ical thinking, and problem-solving skill of the interviewee. So, in this section, we are go-ing to...
3 min read
In Java programming, there are numerous records systems which might be crucial for storing and manipulating data correctly. One such facts shape is the ArrayList, that is a dynamic array that can develop or lessen as desired. In a few times, we can stumble upon conditions...
4 min read
Red Black Tree is a special type of binary search tree that has self-balancing behavior. Each node of the Red-Black Tree has an extra bit, which is always interpreted as color. In order to maintain the balancing of the Red-Black Tree during insertion, updation, and deletion,...
8 min read
Padovan sequence as a tremendous and innovative mathematical sequence that differential from the well-known Fibonacci sequence although has its strong relation. It is said to be defined by a recurrence relation based on the fact that any term is the sum of two terms of the...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India