Posts

Showing posts with the label new

Creating Objects

Image
As we have seen earlier a class provides the blueprint for objects. A class creates a logical framework that defines the relationship between its members. An object in Java is essentially a block of memory that contains space to store all the instance variables. It can also be considered as a variable. Obtaining objects of a class is a two-step process   Declaring  variable of the class type Acquiring an actual, physical copy of the object and assign it to that variable. Box mybox = new Box(); The above line creates an object of the Box class. Here it combines two statements into a single statement. It can be rewritten as - Box mybox; //declare reference to object mybox = new Box(); //allocate a box object Here, first line declares mybox as a reference to an object of type Box . After this line executes, mybox contains the value null. Any attempt to use mybox at this point may cause a compile-time error. S...

YouTube