mino
mino

Reputation: 7298

Java - Object declaration and initialisation

When constructing a new object, I use the following code. In this code, is the object reference the variable 'a'?

BankAcc a = new BankAcc();

Also, out of interest, if the above constructs a new object to the variable a, what does the following do? Does it just create a new object without a variable/object reference?

new BankAcc();

Thanks!

Upvotes: 7

Views: 14640

Answers (9)

lasya
lasya

Reputation: 1

Object is a real time entity or real world entity. Examples of object is pen,car,board,table,pencil etc.,

Suppose see your class name is Book.here how to we declare object

Book obj=new Book();

Upvotes: -5

T I
T I

Reputation: 9943

I think Sun some it up pretty nicely,

"The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor."

So yes a is a reference to the object BankAcc you instantiated with the new operator i.e. you are assigning = the return of new to the variable a which is a reference to an instance of type BankAcc

Regarding your second point, this can be done (compiled and executed) but I can't see many reasons why you would unless the constructor did something 'important'.

Edit: please refer to Tudor's answer for examples where instantiating without a reference is applicable

Upvotes: 0

Robin
Robin

Reputation: 36621

As explained in this Java tutorial, the creation of an object by using

BankAcc a = new BankAcc();

is a multi-step process. You have the declaration, instantiation and the initialization step

I will only highlight the most interesting parts from that tutorial which are relevant to your question:

  • To declare a variable, you use type name; (in this case BankAcc a;), which indicates a will/can be used to refer to data of type BankAcc. At this moment, a does not reference any object

  • To instantiate ayou use the new keyword. This will allocate memory for a new object and returning a reference to that memory. The new operator needs one single postfix argument: a call to a constructor. Whether or not you assign the reference returned by calling new to a variable or not is your choice. You can also use this reference directly in an expression (e.g. new Rectangle().height;)

  • The initialization is the actual call to the constructor, which initializes the new object. The constructor is invoked by the new operator

Upvotes: 6

Keldon Alleyne
Keldon Alleyne

Reputation: 2130

new BankAcc() creates the object and calls the constructor. Sometimes you need to do that, for example:

Rectangle rect = new Rectangle(new Point(100, 200), new Dimension(100, 300));

That is just an example of code so that you can see how it can be used.

Upvotes: 3

Darren Burgess
Darren Burgess

Reputation: 4332

Yes the variable reference is "a" and yes new BankAcc(); creates a new object with no variable thus making it anonymous.

Upvotes: 0

aleroot
aleroot

Reputation: 72686

The second line of code instantiate an anonymous instance of BankAcc class.

It is a quick way to instantiate a class,it is usually used when you need the reference only one time, for example to pass a class instance to a method argument :

myFunc(new BankAcc());

Upvotes: 1

ziesemer
ziesemer

Reputation: 28707

Yes, and yes.

Note that just creating a new instance of a class without holding a reference to it is not unheard of (though not necessarily ideal), as the constructor of the class may do everything that is expected for a given operation.

Upvotes: 1

kosa
kosa

Reputation: 66677

Yes, it just creates object, but it won't be assigned to any reference, so you can't access that object and do any operations on that object. It will just stay there in memory until garbage collected.

Upvotes: 1

Tudor
Tudor

Reputation: 62469

Yes and yes.

The second one might be useful when you just want to use an anonymous object without caring about having a reference. Like:

new Thread(new Runnable() {
     public void run () { }
}).start();

Upvotes: 8

Related Questions