The creation of new object is done by calling special methods called constructors.
Syntax:
new className(parameters)
A certain class can have many constructors, which differ in the number and/or types of their parameters (overloading of constructors).
Semantics:
The invocation of a constructor constructs a new object of the class to which the constructor belong and returns a reference to the created object. The object is constructed by making use of the parameters passed to the constructor.
Example:
new String("test")
The expression constructs a new object of the class String that is equal to the string denoted by "test". The reference to such an object is returned by the expression.
Example:
public class Hello { public static void main(String[] args) { String s = new String("hello world"); System.out.println(s); } }
Note that: