aspose file tools*
The moose likes Beginning Java and the fly likes array of an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array of an object" Watch "array of an object" New topic
Author

array of an object

mark I thomas
Ranch Hand

Joined: Apr 07, 2008
Posts: 86


Do I still have to do obj[0]= new MyObject(); and why ?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

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();


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tony Fu
Greenhorn

Joined: Sep 04, 2008
Posts: 10
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


Do everything by yourself!
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
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?


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Tony Fu
Greenhorn

Joined: Sep 04, 2008
Posts: 10
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
Bartender

Joined: Oct 14, 2005
Posts: 18649
    
    8

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

Joined: Apr 04, 2012
Posts: 924
    
    1

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();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array of an object