In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI).Transient variables − The values of the transient variables are never considered (they are excluded from the serialization process). i.e. When we declare a variable transient, after de-serialization its value will always be null, false, or, zero (default value).Therefore, While serializing an object of a class, if you want JVM to neglect a particular instance variable you need can declare it transient.public transient int limit = ... Read More
Class/static variables belong to a class, just like instance variables they are declared within a class, outside any method, but, with the static keyword.They are available to access at the compile time, you can access them before/without instantiating the class, there is only one copy of the static field available throughout the class i.e. the value of the static field will be same in all objects. You can define a static field using the static keyword.If you declare a static variable in a class, if you haven’t initialized it, just like with instance variables compiler initializes these with default values ... Read More
No, you cannot make the elements of an array immutable.But the unmodifiableList() method of the java.util.Collections class accepts an object of the List interface (object of implementing its class) and returns an unmodifiable form of the given object. The user has only read-only access to the obtained list.And the asList() method of the ArrayList class accepts an array and returns a List object.Therefore, to convert an array immutable −Obtain the desired array.Convert it into a list object using the asList() method.Pass the obtained list as a parameter to the unmodifiableList() method.Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.List; public class UnmodifiableExample ... Read More
A constant variable is the one whose value is fixed and only one copy of it exists in the program. Once you declare a constant variable and assign value to it, you cannot change its value again throughout the program.Unlike other languages java does not support constants directly. But, you can still create a constant by declaring a variable static and final.Static − Once you declare a variable static they will be loaded into the memory at the compile time i.e. only one copy of them is available.Final − once you declare a variable final you cannot modify its value ... Read More
If you declare a variable static and final you need to initialize it either at declaration or, in the static block. If you try to initialize it in the constructor, the compiler assumes that you are trying to reassign value to it and generates a compile time error −Example Live Democlass Data { static final int num; Data(int i) { num = i; } } public class ConstantsExample { public static void main(String args[]) { System.out.println("value of the constant: "+Data.num); } }Compile time errorConstantsExample.java:4: error: cannot assign a value to ... Read More
A final variable which is left without initialization is known as blank final variable.Generally, we initialize instance variables in the constructor. If we miss out they will be initialized by the constructors by default values. But, the final blank variables will not be initialized with default values. So if you try to use a blank final variable without initializing in the constructor, a compile time error will be generated.Example Live Demopublic class Student { public final String name; public void display() { System.out.println("Name of the Student: "+this.name); } public static void main(String args[]) { ... Read More
Whenever you make a method final, you cannot override it. i.e. you cannot provide implementation to the superclass's final method from the subclass.i.e. The purpose of making a method final is to prevent modification of a method from outside (child class).In inheritance whenever you extend a class. The child class inherits all the members of the superclass except the constructors.In other words, constructors cannot be inherited in Java therefore you cannot override constructors.So, writing final before constructors makes no sense. Therefore, java does not allow final keyword before a constructor.If you try, make a constructor final a compile time error ... Read More
When you try to hold the super class’s reference variable with a sub class object, using this object you can access the members of the super class only, if you try to access the members of the derived class using this reference you will get a compile time error.Example Live Demointerface Sample { void demoMethod1(); } public class InterfaceExample implements Sample { public void display() { System.out.println("This ia a method of the sub class"); } public void demoMethod1() { System.out.println("This is demo method-1"); } public static void main(String args[]) ... Read More
When you try to handle an exception (checked) thrown by a particular method, you need to catch it using the Exception class or super class of the Exception occurred.In the same way while overriding the method of a super class, if it throws an exception −The method in the sub-class should throw the same exception or its sub type.The method in the sub-class should not throw its super type.You can override it without throwing any exception.When you have three classes named Demo, SuperTest and, Super in (hierarchical) inheritance, if Demo and SuperTest have a method named sample().Example Live Democlass Demo { ... Read More
The toArray() method of the LinkedList class converts the current Linked List object into an array of object type and returns it. This array contains all of the elements in this list in proper sequence (from first to last element). This acts as bridge between array-based and collection-based APIs.Therefore, to convert a LinkedList to an array −Instantiate the LinkedList class.Populate it using the add() method.Invoke the toArray() method on the above created linked list and retrieve the object array.Convert each and every element of the object array to string.Example Live Demoimport java.util.Arrays; import java.util.LinkedList; public class LinkedListToArray { public static ... Read More