Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class not getting compiled

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





Can anybody please tell me why the above code is not getting compiled ??
 
Shanmugam nagaraj
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ravi,
I think the name of the class cannot be "Main". check with different name.

Thanks
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes , thank you , i forgot that.
 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you doubtless know why String doesn't implement Cloneable.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why there's no need to say new String("xxx").
 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Pavan wrote:Please tell me why doesn't it so ??
Have you got any ideas yet?
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best to continue that discussion on the correct thread.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob and Campbell for the explanation - Certainly cleared up a few things

 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Mercs wrote:Thanks Rob and Campbell . . .
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic