To make it explicite: if you write your own constructor, java will not create the default constructur. So if you need a constructor with arguments and a constructor without arguments (like the default constructor), the you have to write both!
overloaded constructors - as a result of overloading methods and constructors, some may be private and some public. Especially in case when there is a non-public class that you use in your constructors, you may create a public constructor that creates an instance of that class and then passes it to a private constructor.
64 You can make a deep copy with serialization without creating files. Your object you wish to deep copy will need to implement serializable. If the class isn't final or can't be modified, extend the class and implement serializable. Convert your class to a stream of bytes:
51 Constructors are used to initialize the instances of your classes. You use a constructor to create new objects often with parameters specifying the initial state or other important information about the object From the official Java tutorial: A class contains constructors that are invoked to create objects from the class blueprint.
It doesn't do any good to just mark the constructor private; a really determined user may always use reflection to obtain the constructor. Valid uses: One good use of a protected constructor is to force use of static factory methods, which allow you to limit instantiation or pool & reuse expensive resources (DB connections, native resources).
You simply pass the arguments up the constructor chain, like method calls to super classes, but using super (...) which references the super-class constructor and passes in the given args.
Update: Java 8 includes the class java.util.Optional out-of-the-box, so there is no need to use guava for this particular reason in Java 8. The method name is a bit different though. Builder pattern The builder pattern is used for constructors and is implemented by introducing a separate Builder class:
In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory. It is a special type of method which is used to initialize the object.
1 Java provides you with a default constructor which takes no parameters. The constructor also has no body, so it is something like so: public Person() {}. The moment you define you own constructor, your constructor (s) take place of the default one, so in your case, Person(int nm){} takes place of Person() { }.
A default (no-argument) constructor is automatically created only when you do not define any constructor yourself. If you need two constructors, one with arguments and one without, you need to manually define both.