Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Create an array of object references? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Create an array of object references?" Watch "Create an array of object references?" New topic
Author

Create an array of object references?

James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 277

Create an array of object references of the class you created in Exercise 2, but don�t actually create objects to assign into the array.
Code from exercise 2


Here's what I thought is meant, but I think I am wrong.


[ July 23, 2007: Message edited by: James Hambrick ]

Visit my blog! http://jameshambrick.com
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 318
ch4ex1 [] a = {new ch4ex1(),new ch4ex1(" test")};


well, that line sort of fulfills the assignment then goes on to violate it

the first half ch4ex1 [] a would start the declaration of the array well
BUT

the second half {new ch4ex1(), new ch4ex1(" test ")} causes it to be
instantiated with two elements but then goes on
to create two new objects and initializes the two
slots with them.

There is another way to finish that declaration that instantiates the array but does NOT initialize it. See if you can dig that up and that will answer your question. At least I think so....


------------------------
Bob
SCJP - 86% - June 11, 2009
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 277

I though that when you did not use "new" it did not initalize it. but ch4ex1 c, that is not right either.
[ July 23, 2007: Message edited by: James Hambrick ]
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 277

I searched and searched and could not find anything on it, so I started fooling around with it and came up with.


no it does not fire off the constructors, that was the whole point of the lab BTW.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

I think you're all reading too much into this. There's a much simpler way to do it. It involves using the numeral "2" -- if, in fact, you want there to be 2 elements in the array.


[Jess in Action][AskingGoodQuestions]
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 277

The number 2???

ch4ex1[1] = c;
ch4ex1[2] = d;

???
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

No, remember it said "don�t actually create objects to assign into the array"? So don't assign anything to the array elements, like those two lines of code do. Remove them and you're just about done (except for actually declaring an array with two elements).
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
A String is also a object, right?
How do you create an array of Strings with a lenght of 2 but don't assign string content to the array? with object references is the same process.

Hope it helps!


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 318
I am NOT trying to be sarcastic.... please don't interpret it that way... I am trying to help to lead you to the answer.

Go back to whatever text you have chosen for your Java training. Find the place where they very first mention arrays. They typically cover declaration, instantiation, assignment to the elements, and THEN declaration with initialization. Read right through that area and you will find it. And "simplicity is key"!
[ July 23, 2007: Message edited by: Bob Ruth ]
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 277

It said

Create an array of object references of the class you created in Exercise 2, but don�t actually create objects to assign into the array. When you run the program, notice whether the initialization messages from the constructor calls are printed.
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 318
(Mods.... if I sin a great sin, slap me gently and let me know....)

That was the assignment question that you are working on... I meant go back into the chapter reading area on arrays. Here is a link to what I am talking about.....

http://java.about.com/od/beginningjava/l/aa_array.htm

when you get there scroll down to Java Array Initialization and read that section. The first examples are the one I want you to see most. Then they show an alternate form.... THAT is the one you are trying to use here.

The difference between those two forms is that the latter one example instantiates the objects and puts them into the array. That is what your assignment is telling you NOT to do. Read that first paragraph and look at the first three examples.... THOSE create the array without creating and plugging in the elements.
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 277

not sure how to create an array with the length of two without placing information it. My book says that you cannot set the size of an array in Java, it goes on to describe that the compiler basically decided how big to make it when data is entered.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9956
    
    6

What book are you using?

There are two ways to create an array, and I've never seen a book not list both. If there is a book that only talks about one way, many folks here would be interested in knowing what that is.

If you read the page that Bob Ruth linked to, it lays them all out. pay special attention to the "java array declaration" paragraph and the "Java array initialization" paragraph.


Never ascribe to malice that which can be adequately explained by stupidity.
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 277

okay so the Thinking in Java 3rd edition free ebook is wrong then. It says that I cannot create an array and specify the size of it. Said the compiler did that, kept from having an index out of bounds. I still don't get the idea of createing an array of object references. Doesn't seem like I did that.

say this at the beginning
"The compiler doesn�t allow you to tell it how big the array is. This brings us back to that issue of �references.�"

The he uses this example way down towards the bottom of the page
int[] a = new int[rand.nextInt(20)];



[ July 24, 2007: Message edited by: James Hambrick ]
[ July 24, 2007: Message edited by: James Hambrick ]
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 318
OKay, James......thanks for that reference to the Thinking in Java book. NOW I see where you are coming from.

If I can speculate, I am going to call that an "enforced suggestion". I went to my copy and read the whole part that you are talking about and NOW I see why I am probably causing more confusion than anything else for you.

I have to think that the author left this out just to make it easy for you but, trust me, you can do it!

Object [] obj1; // declares a reference to an array of Objects
// I dont know how many as we have not yet
// allocated storage.

obj1 = new Object[20]; //NOW I have created an array in memory
// that can hold 20 references to objects
// of type object. By default they are
// all set to null . I have not created
// an object yet, well other than the array
// itself but all 20 elements of the array
// are null and NOT pointing to an Object.


Object [] obj2 = new Object[20];
// This form does the exact same thing in one line. The declaration
// and the allocation of the array storage of 20 Object references
// all set to null.

The above is the basic form for creating an empty array of objects of type Object.

It appears the Mr Eckels is strongly recommending the style of pre-allocating the Objects themselves and initializing the array with their references at compile time, allowing the compiler to allocate the storage and define the number of elements. This is a valid and very popular technique. But given that your initial array said to create the array without creating and initializing the references.... the above is the only way you have to do it.

Again, sorry for the confusion and frustration but I hope that you can look at this as an expansion of your knowledge on the subject.

Best o'luck
Bob
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 252

Originally posted by James Hambrick:
The number 2???

ch4ex1[1] = c;
ch4ex1[2] = d;

???


While others have tried (really hard) to explain to you as best as possible, your real source of confusion, I want to explain one point that is not your immediate source of confusion (but is likely to bite you very soon).

Arrays in Java are zero-based. In other words, the first element of an array ch4ex1 is ch4ex1[0]. If you have an array of N elements say MyArray, the Nth element of this array would be MyArray[N-1].

- Anand


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 277

I knew you could do that in C/C++ but if the book said it then I thought I could not in Java. Heard this book was real good for beginners. I have the SCJP book coming to me in a few days so I can study for SCJP. Hard going from VB6 to Java lol.

BTW I knew that the arrays start at 0, but thanks for mentioning it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Create an array of object references?
 
Similar Threads
Static variable initialization
Thread synchronization doubt
objects passed by reference
Array.asList question
Doubt in try and finally block execution.