Why ArrayList add method needs to have new object declared?

I

io

Hi --

I'm reading the Object First With BlueJ Book (this is not homework -
self-taught). I have to iterate through an ArrayList object. This
object represents where I store auction lots. I have to go through
that collection and check if an object has _not_ been sold, in which
case I should store in another ArrayList and return that.
The solution I came up with is this, which works (I also post the other relevvant classes for your reference):




public ArrayList getUnsold()
{
ArrayList unsold;
new unsold();
Iterator it = lots.iterator();
while(it.hasNext()) {
Lot lot = (Lot) it.next();

// if a highestBid wasn't made, then the lot wasn't sold at
the auction.

Bid highBid = lot.getHighest();
if( highBid == null ) {
unsold.add(new Lot(lot.getNumber(), lot.getDescription()));
}
}
return unsold;

}

My question is why I *have* to use "new" in:
unsold.add(new Lot(lot.getNumber(), lot.getDescription()));

instead of

unsold.add(Lot(lot.getNumber(), lot.getDescription()));

which result in an error?

As I understand this, I already had a lot object. Why "new"?
Under the "if" block is there a new lexical scope, and what you effectively have is then a copy of the object created in the line

Lot lot = (Lot) it.next();

Where can I read more about this?

TIA

PM




//////////////////
// Lot class
/////////////////



public class Lot
{
private final int number;
private String description;
private Bid highestBid;
private Person bidMaker;

public Lot(int number, String description)
{
this.number = number;
this.description = description;
}

/**
* Attempt to bid for this lot. A successful bid
* must have a value higher than any existing bid.
* @param bid A new bid.
* @return true if successful, false otherwise
*/
public boolean bidFor(Bid bid)
{
if((highestBid == null) ||
(bid.getValue() > highestBid.getValue())) {
// This bid is the best so far.
highestBid = bid;
return true;
}
else {
return false;
}
}

/**
* @return A string representation of this lot's details.
*/
public String toString()
{
String details = number + ": " + description;
if(highestBid != null) {
details += " Bid: " +
highestBid.getValue();
}
else {
details += " (No bid)";
}
return details;
}

/**
* @return The lot's number.
*/
public int getNumber()
{
return number;
}

/**
* @return The lot's description.
*/
public String getDescription()
{
return description;
}

/**
* @return The highest bid for this lot. This could be null if
* there are no current bids.
*/
public Bid getHighestBid()
{
return highestBid;
}


public String getPerson()
{
return bidMaker.getName();
}
}

/////////////////
// Bid
/////////////////



public class Bid
{
// The user making the bid.
private final Person bidder;
// The value of the bid. This could be a large number so
// the long type has been used.
private final long value;

/**
* Create a bid.
* @param bidder Who is bidding for the lot.
* @param value The value of the bid.
*/
public Bid(Person bidder, long value)
{
this.bidder = bidder;
this.value = value;
}

/**
* @return The bidder.
*/
public Person getBidder()
{
return bidder;
}

/**
* @return The value of the bid.
*/
public long getValue()
{
return value;
}
}
 
T

Thomas Kellerer

io wrote on 10.02.2006 23:27:
My question is why I *have* to use "new" in:
unsold.add(new Lot(lot.getNumber(), lot.getDescription()));

instead of

unsold.add(Lot(lot.getNumber(), lot.getDescription()));

I guess what you want is:

unsold.add(lot);

You want that specific instance of the Lot class to be put into the
unsold class, not a new one.

Thomas
 
M

mike

Yeah. As far as I know, unsold.add(lot); should work. The reason you
have to use new when doing it the other way is because you are actually
calling the constructor or the Lot class. So by doing new
Lot(lot.getNumber(), lot.getDescription()) you are actually creating a
new object. On the other hand, if you just did unsold.add(lot); you
would be passing the lot object to the add method.
 
O

Oliver Wong

io said:
I'm reading the Object First With BlueJ Book (this is not homework -
self-taught). I have to iterate through an ArrayList object. This
object represents where I store auction lots. I have to go through
that collection and check if an object has _not_ been sold, in which
case I should store in another ArrayList and return that.
The solution I came up with is this, which works (I also post the
other relevvant classes for your reference):

There's a couple of "strange" lines of code in here. Strictly speaking,
they might not errors, but they are a sign that some confusion was
happening.
public ArrayList getUnsold()
{
ArrayList unsold;
new unsold();

Here, you're reserving the variable name "unsold" to use to refer to an
ArrayList. However, you haven't actually yet assigned an arraylist to the
variable yet! You've only reserved the name. That means later on, if you do
"unsold.add()" for example, you'll get an error message about unintialized
variables.

The second line says you're creating a new instance of a class called
unsold(). Now maybe you do have a class called unsold, but it seems like a
very strange name for a class. Probably this second line is part of the same
mistake as the first line. I'm guessing you meant something like this:

ArrayList unsold = new ArrayList();

- Oliver
 
I

io

mike said:
Yeah. As far as I know, unsold.add(lot); should work. The reason you
have to use new when doing it the other way is because you are actually
calling the constructor or the Lot class. So by doing new
Lot(lot.getNumber(), lot.getDescription()) you are actually creating a
new object. On the other hand, if you just did unsold.add(lot); you
would be passing the lot object to the add method.

Oh, ok. There's a difference, I suppose, creating new objects on the heap ?

TIA and thanks also to the other guys that answered.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,919
Messages
2,570,037
Members
46,444
Latest member
MadeleineH

Latest Threads

Top