Creating Objects with new
- To create an object in Java, you must call the constructor.
- Use the keyword
new:
Point p1 = new Point();
- This will automatically allocate space for the object and initialize it by
calling the appropriate constructor.
- The number and types of arguments (the signature) determines which
constructor is called.
- If you do not use new, you will end up with instances that point to
the same object in memory, so be careful. (Remember everything in Java is
a reference.)
- Remember, you can only call a constructor with new and you call
other methods with dot-notation (object.method() or Class.method()).