Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array-constructor

 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String[] x = new String[10];
At this line which constructor gets called.
Since we have used new keyword some constructor must be called. which is that? I am not able to figure out, but i know for sure that String constructor is not called.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when new is used like this is that a new array is created. An array is an object in Java, but does not have a class definition like other objects. Space is allocated in memory for the elements of the array. In this case, 10 contiguous spaces are set aside in memory each holding a reference to a String. Before you assign the individual elements of the array to point to actual Strings, each reference is null.
 
Balaji Sampath
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me try to rephrase the question in this way:



From the above code the first constructor to be called would be the default constructor StringCreation() .

I beleive thats the explanation u require?

Anywayz please to brief me if u need any further detail regarding the above code.

Thanks
Balaji.S
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found this thread.
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks balaji,
can you put some more light on StringCreation() constructor.
I mean is this StringCreation a logical class.
 
Badri Sarma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keith Lynn explanation was correct. when you use the following piece of code
String str[] = new String[10];

continious memory allocation would be created. No constructor will be called, at that point.
 
Lukas Stephienn
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keith Lynn explanation was correct. when you use the following piece of code
String str[] = new String[10];

continious memory allocation would be created. No constructor will be called, at that point.


Don't agree with that. Becaue Arrays extends Object, the Object constructor will be called.
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats the point, if object constructor gets called then who calls it.
I believe since array extends object and if object constructor gets called, there must be some constructor calling it.
Or may be no constructor gets called at all.
I am still to find the answer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic