Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array of an object

 
mark I thomas
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Do I still have to do obj[0]= new MyObject(); and why ?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mark I thomas wrote:
MyObject[] obj = new MyObject[10];

Well...what exactly does this do? It creates an Array that can hold objects. You have a container you can put stuff in, but there is nothing in it.

When you then say: obj[0].setName("mark");

you are saying "set the name of the object at position 0 to "mark""...but there is no object there. You have to create the object first. so yes, you DO have to say this first:

obj[0]= new MyObject();
 
Tony Fu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger is right. When you MyObject[] obj = new MyObject[10]; ,the obj contains [null,null,null,null,null,null,null,null,null,null].
So you cannot use null object invoke a method
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you cannot use null object invoke a method

I think I disagree, but I'm not sure exactly what you mean. Can you explain?
 
Tony Fu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Brannon wrote:
So you cannot use null object invoke a method

I think I disagree, but I'm not sure exactly what you mean. Can you explain?

MyObject[] obj = new MyObject[10];

It's array's initialization.And the array contains 10 elements.But each element is null.
 
Paul Clapham
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just one correction: there is no such thing as a "null object". What you have when you use the keyword "null" is a "null reference". That simply means a reference which does not refer to any object.

Or to put it another way: Objects cannot be null, but references can be null.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the important point here is that arrays are initialized to the default values(depending upon their types) no matter where they are declared. so for example if you declare and initialize an int array as int[] a = new int[6], array will be initialized to the default value of int which is zero. so the container will be [0,0,0,0,0,0]. in case of array of objects the default values is null. so when you did MyObject[] my = new MyObject3[]; it creates a 'my' reference variable of the type 'MyObject Array' [ Remember that arrays are objects]. this reference variable references a 'CONTAINER'. this CONTAINER , has 3 places. these 3 places are occupied by REFERENCES OF THE TYPE MyObject. these references will[right now they are not pointing to anything;the references are 'null'] point to Objects of the TYPE MyObject. if you want to assign the reference on the first place out of three places to actual MyObject object then you have to do my[0] = new MyObject();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic