URGENT: Clone method dilemma

M

michael

To newsgroup,

<code snippet>

package folder;
import java.lang.Object;

class Foo{
public static void main (String[] args){
Bar b = new Bar();
Object obj = b.clone();
Object[] array = new Object[10];
array[0] = obj;
}
}

.....
package folder;

class Bar implements Cloneable{
public Bar(){ System.out.println("creating bar object");}
}


Problem: I get a compile-time error complaining that the clone() method is
protected. As you can see I have packaged both classes in the same
package, and I have imported the Object class (even though it should be
automatic),
therefore I should not be getting this error. What am i doing
wrong? note, I am using Java SDK 1.4


Any help appreciated

Regards
programmer
 
S

Sudsy

michael wrote:
Problem: I get a compile-time error complaining that the clone() method is
protected. As you can see I have packaged both classes in the same
package, and I have imported the Object class (even though it should be
automatic),
therefore I should not be getting this error.
<snip>

Problem: this is not URGENT, as your subject implies. If it were, you
would have taken the time to search the archives, read the javadocs,
and discovered that this question has been asked and answered more times
than you could shake a stick at.
You SHOULD be getting the error. You just don't seem to understand why.
I'll include the working source code here for your convenience (since
the classes are small) but that doesn't abrogate your responsibility to
perform the research and understand the issues.

package folder;
import java.lang.Object;

class Foo{
public static void main (String[] args){
Bar b = new Bar();
Object obj = null;
try {
obj = b.clone();
}
catch( CloneNotSupportedException e ) {
e.printStackTrace();
System.exit( 12 );
}
Object[] array = new Object[10];
array[0] = obj;
}
}

package folder;

class Bar implements Cloneable{
public Bar(){ System.out.println("creating bar object");}
public Object clone() throws CloneNotSupportedException {
return( super.clone() );
}
}
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top