To create a new instance of an object, we use the "new" keyword. This keyword creates a new instance of an object, which we can then assign to a variable, or invoke methods. For example, to create a new StringBuffer object, we would use the new keyword in the following way.
StringBuffer myBuffer = new StringBuffer (50);
Notice how we can also assign parameters (in this case, we are allocating at least fifty characters for our buffer). If we don't wish to specify any parameters, we could use the new keyword this way.
StringBuffer myBuffer = new StringBuffer ();
The new keyword expects a class name, followed by the parameters that will be passed to a constructor. When we have no parameters, we simple use empty ()'s.
Posted in: Java