First create a singleton class. If we will try to clone it, it will throw Exception. Now try to break the Singleton with Reflection How to prevent Object Creation with Reflection - Check in the private constructor if object already created then throw an exception. If we run the reflection code, we will get this exception. Prevent Singleton Class from Serialization We have to override the read resolve method
The purpose of Optional is not to replace every single null reference in your codebase but rather to help design better APIs in which—just by reading the signature of a method—users can tell whether to expect an optional value. In addition, Optional forces you to actively unwrap an Optional to deal with the absence of a value; as a result, you protect your code against unintended null pointer exceptions. String version = computer.getSoundcard().getUSB().getVersion(); This code can lead to NullPointerException. Handle NullPointerException - This code looks ugly. String version = "UNKNOWN"; if(computer != null){ Soundcard soundcard = computer.getSoundcard(); if(soundcard != null){ USB usb = soundcard.getUSB(); if(usb != null){ version = usb.getVersion(); } } } String name = computer.flatMap(Computer::getSoundcard) .f...
Comments
Post a Comment