SE450: Java: Object Creation [20/22] ![]() ![]() ![]() |
How are objects specified and created?
Prototype-based languages allow the static description of objects (as literal values). To create new objects at runtime, they use cloning. JavaScript is an example of a prototype-based language. It doesn't distinguish between classes and instances. Any object can be used to create a new object by cloning it.
In JavaScript, an object can go from String to Integer to String without being re-instantiated.
Class-based language allow the static description of classes. To create objects at runtime, use a constructor.
When a reference type is created, only a storage location for the reference is made, not the objects themselves.
Point p; // reference variable of type Point p = new Point(); // new Instance of point int[] ia; // reference variable of array of integers ia = new int[3]; // an array of size 3 Point[] pArray; // reference variable of array of Points pArray = new Point[3]; // an array of Points. They are all null! // can use an array initializer ia = {1,2,3}; pArray = { new Point(), new Point() } ;