How to create object in Java


To  Create object in java

                         Let us consider a class Sampleclass. If object needs to be created for this class it can be done by means of  keyword   "new".

Using "new" keyword, object for a class can be created.

class Sampleclass{

}


Object of the above class can be created as follows.


Sampleclass exp=new Sampleclass();


here exp is the name of the object that is created for the class Sampleclass. 

  new keyword creates the object for the respective class and allocates memory for the object in heap.
Previous
Next Post »