PDA

View Full Version : Java clone() method help



Sidneyaks
October 28th, 2010, 06:20 PM
Ok, so I'm trying to use the GregorianCalendar (http://download.oracle.com/javase/1.5.0/docs/api/java/util/GregorianCalendar.html) class's clone() method, which as I understand it from the API overrides the clone() method from the object class.

However, the GregorianCalendar.clone() (http://download.oracle.com/javase/1.5.0/docs/api/java/util/GregorianCalendar.html#clone()) returns an object type.

Do I need to explicitly downcast in my own code (and if so, how?), or is the clone() method just relatively useless? I've read most everywhere that down casting is a terrible idea in 99.9% of all cases, but this just seems a little strange to me.

By the way, I'm trying to use it like this


public class event
{
private GregorianCalendar calStart; //Declaration of calStart
...some other woring code here...
public setCalStart(GregorianCalendar a)
{
if(a == null)
{
System.out.println("Fatal error, reference to GregorianCalendar object in null");System.exit(0);
}
calStart = a.clone()
}

}


Thanks,
Sidney

P.S. I'll admit it right now, this is help with a homework assignment, but I'm not asking for help with the homework, just asking about java in general.

dwhitney67
October 28th, 2010, 06:43 PM
Please show an example where you are using clone(). copy() is not the same.

Here's a basic example:


import java.util.GregorianCalendar;

class Cal
{
private GregorianCalendar calStart;

public Cal()
{
calStart = new GregorianCalendar(2010, 10, 28);
}

public GregorianCalendar getNextDay()
{
GregorianCalendar nextDay = (GregorianCalendar) calStart.clone();
nextDay.add(GregorianCalendar.DAY_OF_MONTH, 1);
return nextDay;
}

public static void main(String[] args)
{
Cal c = new Cal();
GregorianCalendar gc = c.getNextDay();
}
}

Sidneyaks
October 28th, 2010, 06:51 PM
Oh! Sorry, I meant to write a.clone() but wrote a.copy().

I edited my original post accordingly, sorry for that.

Also, thank you for the example on casting.