aspose file tools*
The moose likes Beginning Java and the fly likes class not getting compiled Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class not getting compiled" Watch "class not getting compiled" New topic
Author

class not getting compiled

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234






Can anybody please tell me why the above code is not getting compiled ??


Save India From Corruption - Anna Hazare.
Shanmugam nagaraj
Ranch Hand

Joined: May 07, 2007
Posts: 76
Hi Ravi,
I think the name of the class cannot be "Main". check with different name.

Thanks


Thanks <br />Shanmugam N
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3859

Ravi wrote: Can anybody please tell me why the above code is not getting compiled ??


What happen when you try to compile? Did you check the error?

Shanmugam wrote:I think the name of the class cannot be "Main". check with different name.


No, That's not a problem.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ravi Pavan wrote:


String str2 = (String) str1.clone();
Can anybody please tell me why the above code is not getting compiled ??


your class need to implements Cloneable interface
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Yes , thank you , i forgot that.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
And you doubtless know why String doesn't implement Cloneable.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

And why there's no need to say new String("xxx").
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
David Newton wrote:And why there's no need to say new String("xxx").
Yes, he does know that. Look here
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

seetharaman venkatasamy wrote:
Ravi Pavan wrote:

String str2 = (String) str1.clone();
Can anybody please tell me why the above code is not getting compiled ??

your class need to implements Cloneable interface


I am completely lost out here. How does implementing the Cloneable interface for the Main class help - it still would not make String Cloneable would it?

Campbell Ritchie wrote:
And you doubtless know why String doesn't implement Cloneable.

No idea here. Why not?


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Campbell Ritchie wrote:And you doubtless know why String doesn't implement Cloneable.


Please tell me why doesn't it so ??
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Sam Mercs wrote:
I am completely lost out here. How does implementing the Cloneable interface for the Main class help - it still would not make String Cloneable would it?


Here they meant String class and not Main class.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Ravi Pavan wrote:Please tell me why doesn't it so ??
For the same reason you don't need a copy constructor for the String class.

A quick perusal of the API (you only need to read about 5 sentences) will give you a hint. Then you can tell us what you think!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Ravi Pavan wrote:Please tell me why doesn't it so ??
Have you got any ideas yet?
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Then you can tell us what you think


sure ,

The String class does not allow you to clone String objects because Java has a policy where it shares a single String object among multiple references, and cloning violates that policy.

Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Campbell Ritchie wrote:For the same reason you don't need a copy constructor for the String class.


The String class does have a copy constructor right? Frankly I still don't get the reason why the String class should not have been cloneable?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Sam Mercs wrote:
Campbell Ritchie wrote:For the same reason you don't need a copy constructor for the String class.


The String class does have a copy constructor right?

Yes it does. There is the String(String) constructor.

Frankly I still don't get the reason why the String class should not have been cloneable?

Because you will never need to clone strings. They are immutable, so their contents can never change. If String would have been Cloneable, you could clone an immutable String, and you would get another object that will forever be exactly equal to its source. Cloneable only makes sense for objects that can change, and you want to "fork" the object at a specific state.

Now logically, the copy constructor wouldn't be necessary either, but I think they let it exist for reflection issues. It surely isn't needed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Rob beat me to it with his answer. Agree that it is because of immutability that there is no need for String to implement Cloneable. I suspect the real reason for the String(String) copy constructor is because in the early days of Java somebody thought it was a good idea, and it is now impossible to remove so as to maintain backward compatibility. When you create "real" classes, you want to keep their public interface as small as possible.

But String is a "library" class; they would give it a large public interface, including things like the copy constructor, even though 99.9% of users can see no point in using it, just in case somebody wants it.

And while we're on about copy constructors, did you work out why you weren't getting == to return true on Strings on your other thread?
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

And while we're on about copy constructors, did you work out why you weren't getting == to return true on Strings on your other thread?



This is the question i wanted to ask , please tell me the reason why it will return false ??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Best to continue that discussion on the correct thread.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Thanks Rob and Campbell for the explanation - Certainly cleared up a few things

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Sam Mercs wrote:Thanks Rob and Campbell . . .
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class not getting compiled