permaculture playing cards*
The moose likes Beginning Java and the fly likes Concept of Instantiate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Concept of Instantiate " Watch "Concept of Instantiate " New topic
Author

Concept of Instantiate

Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Hello everyone

I just gave an interview recently and faced a weird question on making an instance of a class.
Say, we are making an instance of a class A as follows :
A a = new A();

Que: What is meant by the " A() " term ? Though i know " new A() " will call the constructor of that class but he excluded the "new" in his question.

Thanks!!
Ahsan Bagwan
Ranch Hand

Joined: Oct 05, 2010
Posts: 157
A() might be the default constructor (a no-arg constructor implicitly generated when no constructor is provided) or could be an explicitly provided no-arg constructor of the class. Since it cannot stand on its own when creating objects, you do need a new operator as you have already figured out. You then get reference to the object you just created and can then assign it to an object type, or use as it is like -



Please read http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html (unless I am mistaken what your question was).

If he excluded new, the compiler will reject the program.
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
ok thanks Ahsan!!!
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Agree with Ahsan, compile error occur without new operator. However, just to add, there are some object which can be created without new operator, for instance, String and array.


http://muhammadkhojaye.blogspot.com/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Ahsan Bagwan wrote: . . . the default constructor (also called as no-arg constructor) of the class. . . .
That is confusing. A default constructor might have no arguments, but a default constructor and an ordinary no‑arguments constructor are different.
Ahsan Bagwan
Ranch Hand

Joined: Oct 05, 2010
Posts: 157
Thanks for the heads up Campbell. I have worded the sentence differently.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Thank you.
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Campbell Ritchie wrote:Thank you.


Thank you Campbell!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Concept of Instantiate
 
Similar Threads
Threads share the memory space
Creating an object from a static method
Singelton Class
StackOverflowException
declare inner class