File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array of an object" Watch "array of an object" New topic

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

Joined: Oct 02, 2003
Posts: 10906

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

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

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

Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Tony Fu

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

Joined: Oct 14, 2005
Posts: 18110

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: 923

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

OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
I agree. Here's the link:
subject: array of an object
Similar Threads
JComboBox Selection?
About Object Serialization (Confusion)
Transitive Object Serialization
Why does it take 2 requests to set session attribute?
Using static