Project Management Professional: Core Java Interview questions!

Tuesday, July 21, 2020

Core Java Interview questions!


9.Can constructors be synchronized in Java ?

Ans. No. Java doesn't allow multi thread access to object constructors so synchronization is not even needed.



10. Can we declare static variables as transient ?

Ans. It's weird that compiler doesn't complain if we declare transient with static variable because it makes no sense. At least a warning message saying "transient is useless in this situation" would have helped with code cleaning.


Static variables are never serialized and transient is an indication that the specified variable shouldn't be serialized so its kind of double enforcement not to serialize.


It could be that as it makes no different to the variable behavior and hence using both keywords with a variable are permitted.


11. What is transient ?

transient is a Java keyword which marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes.


No comments: