Skip to main content

Posts

Inner Class in Java

Inner class in Java In this article, we are going to learn a special type of class which is known as the Inner class in Java. Let's start this tutorial with the definition of the inner class, then we'll look at the types of the inner class, and how to create and use them. What is Inner class in Java? An inner class is a class that is defined within another class. An inner class can be considered as a member of the outer class and like the other members, it can have any access specifier and can be declared as abstract or final. There are generally two types of inner class in java: Static inner class or Static class Non-Static Inner class Whenever the inner class is marked with the static keyword, it will be called a Static class in java. Otherwise non-static inner class. In this tutorial, we are going to discuss the non-static inner class in java, so whenever we refer to the inner class it means we are talking about the non-static inner c
Recent posts

Static Class in Java

Static class in Java In this article, we are going to study what is a Static class in java, how we can create it and the benefits of making a static class. Before starting our tutorial on the static class, we first want to briefly remind you that "What is static?" , static is a keyword in java which is used to define a static member. A static member-created only once for a class and referred directly by the class name itself, objects created by the class can also refer the static member but all the objects share the same reference as static member-created only once for a class. A static member can be: static variables, static methods, static block or static initialization block, and, static class In this article, we will learn all about the static class in java, and we will discuss the other static members in another article. So, let's start this tutorial with the de

Immutable Class in Java

Immutable Class in Java In this article, we are going to learn how to create an immutable class in java. Immutable class is a class which constructs immutable objects. The state or property of an immutable object can only be initialized once (only in the constructor), and after that, all the properties of an immutable object remains constant throughout the life of the object. Before implementing immutable class in java, we must read the rules/guidelines provided for creating immutable class in java. Rules to create Immutable class in Java All the rules to create an Immutable class in java is defined to make sure that the state or property of the object do not get modified anyhow. Look at the below-mentioned rules to create an Immutable class in java. Class should be final : by making a class as final, we can ensure that the class will not to be extend by any other class and the methods will not allow to get overridden by the s

Serialization in Java

Serialization in Java In this article, we are going to learn how to accomplish Serialization in java and the importance of serialization. Also, we will learn the reverse of serialization i.e., Deserialization . What is Serialization in java? Serialization is a process that is used to convert an object into the stream of bytes so that the stream data bytes can be transferred to any location outside the java like databases, network transmission, files, etc. Deserialization in java Deserialization is the reverse of serialization in which the stream of bytes is converted back into an object. This is majorly used to fetch data from databases, read files or received data packets from the network and deserialized into an object within the java memory space. How to do serialization in java? To achieve serialization in java, we need to use a marker interface named as Serializable. Serializable interface is the part of the java.io package and If any object needs to be serial

Serialize HashSet in Java

How to serialize hashset in Java In this article, we are going to learn how to serialize HashSet in Java. Also, we will see how to deserialize HashSet in Java. To serialize or deserialize, the class must implement the java.io.Serializable interface. In our case, we can serialize HashSet as HashSet class implements Serializable Interface, See the hierarchy of HashSet . "Serialization is a process in which we convert the object into the byte stream and this stream of bytes can be stored into the file system/databases or can be placed onto the network for transmission from one place to another. Deserialization is the reverse process of the Serialization. Deserialization is to get back the objects from the byte stream." Table of Content 1. How to serialize HashSet of strings? 2. How to serialize HashSet of user-defined objects? How to serialize HashSet of strings? Let's start with a very basic example to serialize HashSet of string objects. We can

Different ways to iterate HashSet in java

How to Iterate HashSet in java In this article, we are going to learn about how to iterate HashSet in java. Java provides various ways to loop over the HashSet that we are going to see one-by-one. What is HashSet in java? HashSet in java is an implementation of the Set interface of Java. A Set is similar to a List but duplicate elements are not allowed in a Set. To know more about the HashSet, visit this article: HashSet in java Table of Content 1. How to iterate HashSet in java using Iterator? 2. How to iterate HashSet in java using for each loop? 3. How to iterate HashSet in java using forEach() method? How to iterate HashSet in java using Iterator? HashSet provides a method called iterator() which returns an object of Iterator. Iterator is an interface defined in java.util package and its implementation are provided by the HashSet internally. Look at the Syntax of how to get the object of Iterator from the HashSet: Syntax of iterator() method Iter

How to synchronize HashSet in java

How to synchronize HashSet in java? In this article, we are going to understand how we can synchronize HashSet in Java. As we know, HashSet is not synchronized in java i.e., it is not thread-safe . So we must know how to make a HashSet synchronized so that we can use it in the multi-threaded environment. Why HashSet is not Synchronized? First, we should know "Why HashSet is not Synchronized?" before knowing how to make that synchronized. HashSet is a widely used Collection whenever we need to use an implementation of the Set interface. If HashSet would be made as synchronized then it would be much slower as compared to the current implementation of HashSet. Because to make any class synchronized, it needs performance overhead. Therefore, the developers of HashSet made it as non-synchronized to work faster and provide other ways to make it synchronized. There are two ways to synchronize HashSet in Java: Using Collections.synchronizedSet() method Using Cop