Java - declaring an object and initializing it

When creating a new object, I use the following code. In this code, an object reference to the variable 'a'?

BankAcc a = new BankAcc(); 

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

 new BankAcc(); 

Thanks!

+7
source share
9 answers

Yes and yes.

The second can be useful when you just want to use an anonymous object without worrying about having a link. How:

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

As explained in this Java tutorial , creating an object using

 BankAcc a = new BankAcc(); 

- multistage process. You have an declaration, instance, and initialization step

I will focus only on the most interesting parts of this tutorial that are relevant to your question:

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

  • An instance of a uses the new keyword. This will allocate memory for the new object and return a link to that memory. The new operator needs one postfix argument: a constructor call. Regardless of whether you assign a link called by calling new to a variable or not, this is your choice. You can also use this link directly in the expression (for example, new Rectangle().height; )

  • initialization is the actual call to the constructor that initializes the new object. The constructor is called by the new operator

+6
source

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

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

This is just sample code so you can see how it can be used.

+3
source

Yes, it just creates an object, but it will not be attached to any link, so you cannot access this object and perform any operations on this object. He will remain there until the garbage collection.

+1
source

Yes and yes.

Please note that simply creating a new instance of the class without reference to it is not unheard of (although not necessarily ideal), since the class constructor can do everything that is expected for this operation.

+1
source

The second line of code is an anonymous instance of the BankAcc class.

This is a quick way to instantiate a class; it is usually used when you need a reference only once, for example, to pass an instance of a class to a method argument:

 myFunc(new BankAcc()); 
+1
source

Yes, a reference to the variable "a" and yes new BankAcc(); creates a new object without a variable, which makes it anonymous.

0
source

I think Sun is a little beautiful,

"The new operator creates an instance of the class by allocating memory for the new object and returning a reference to that memory. The new operator also calls the constructor of the object."

So, yes a is a reference to the BankAcc object that you created using the new operator, i.e. you assign = return new to variable a , which is a reference to an instance of type BankAcc

As for your second point, this can be done (compiled and executed), but I don’t see many reasons why you would like it if the constructor did not do something β€œimportant”.

Edit: please refer to Tudor's answers for examples where unlinked copying applies

0
source

An object is a real-time object or a real-world object. Examples of objects are a pen, a car, a board, a desk, a pencil, etc.,

Suppose your class name is Book.here, as we declare an object

Book obj = new book ();

-5
source

All Articles