• Post Reply Bookmark Topic Watch Topic
  • New Topic

a simple question  RSS feed

 
subhadeep chakraborty
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to make a class same as String(immutable, which can be initialized like
String="abc"; // normally we do String a = new String("abc");
Kindly help me out
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you have so far, and what are you struggling with?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you mean that you want to be able to write a kind of String-like class which you could initialize by assigning a string literal, like this:

MyOwnStringlikeClass str = "Hello";

Then that's not possible in Java. The compiler does some special things for class String which do not work with classes that you write yourself.
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for making objects immutable, just don't create any methods that can modify your data, and keep the data private. If you make it protected or use default access, other classes are still able to change the object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!