All classes have at least one constructor. A constructor is used to initialize a new object of that type and has the same name as the class.
A Constructor Is a Special Memeber Function Used To Initialize a New Object Belonging To The Class.

To create a constructor, define a block of code with the same name as the class. For example, the following defines the construtor for a class called “Painting”.

To invoke a constructor, use the “new” keyword, followed by the name of the class.

When creating a Java constructor, remember the Java Constructor Rules.

1) The name of the constructor must be the same as the class name for which the constructor is being defined.
2) The constructor must not define a return type.
3) If a constructor is not defined for a class, the compiler creates a default constructor. This default constructor contains no arguments.
4) If a constructor is defined for class, the compiler does not create a default constructor.
5) Overloaded constructors may be created but there cannot be two constructors in the same class with the same argument list.


class box{

int x;

void area() {



class Boxdemo {

public static void main(String[] args){

box ob1=new box();

box ob2;ob2=new box();



There is a class named ‘box’. And File name is ‘’.There is a object ‘ob1’ Creation at Run Operator Allocates a Memory at Run Time for an Object(ob1) and Return a Reference to it.Here box() is a Default Constructor.

There is Only a Reference Variable ‘ob2’.It create At Compile Time. This ‘ob2’ Contains a Null Value.
It allocates a box Object.Now ‘ob2’Holds The Memory Address of ‘box’ object.
Parameter passing :

This is another common question on Java newsgroups, made worse by the fact that people who should know better still perpetuate the following False:
False: “Objects are passed by reference, primitives are passed by value” Some proponents of this then say, “Ah, except for immutable objects which are passed by value [etc]” which introduces loads of rules without really tackling how Java works. Fortunately the truth is much simpler:
True: Everything in Java is passed by value. Objects, however, are never passed at all. That needs some explanation – after all, if we can’t pass objects, how can we do any work? The answer is that we pass references to objects.
True: The values of variables are always primitives or references, never objects. This is probably the single most important point in learning Java properly. It’s amazing how far you can actually get without knowing it, in fact – but vast numbers of things suddenly make sense when you grasp it.
When people hear the words “pass by reference“, they may understand different things by the words. There are some pretty specific definitions of what it should mean, however. Dale King sometimes quotes this one: “The lvalue of the formal parameter is set to the lvalue of the actual parameter.” (Dale’s formal analysis of this whole question can be found at the bottom of this page.) This would mean that if you wrote the following code:

Object x = null;

giveMeAString (x);

System.out.println (x);

void giveMeAString (Object y){

y = “This is a string”;


the result (if Java used pass-by-reference semantics) would be This is a stringinstead of the actual result: null

Explaining the two true things above eliminates all of this confusion.