Constructor as the name suggests is used to construct something and here in case of JAVA it is used to construct the object of class. This is the first point where the object is assigned memory in heap and its member variables gets initialized by default values.Whenever we create an object by using new() keyword,it internally gives call to the default constructor of class. Even if you don't declare the default constructor , java compiler provides automatically a default constructor. Let us test this out with a example . We have created a java class Student.java with a test class TestConstructor.java as shown below : Student.java TestConstructor.java Here we have created a object of Student class at line 9 using new keyword. This gives call to the default constructor of Student class. Note that we have not defined the default constructor explicitly in Student.java . This is provided by java compiler internally. The equivalent code that java compiler provides is as bel
Sometimes when a object is serialized at one server and deserialized at other server i.e on different JVMs , deserialization may fall and you can come across InvalidClassException . This seems to be quite weird. Right? This post is aimed at explaining the root cause of this issue and what should be the better approach while implementing serialization . You can refer to my other blog on Serialization where we have learnt the serialization mechanism and how to implement serialization in Java to get started on serialization. Basically , when we serialize any object , the class metadata is also saved along with object's state which is used while that object is deserialized. One thing that is part of this metadata is serialVersionUID . In my previous post , I told you that it is used for versioning. We will now see what does it actually mean . You must have seen that whenever you implement serializable interface , compiler gives you warning to generate serialVersionUID( suid )