• Post Reply Bookmark Topic Watch Topic
  • New Topic

Final immutable String class, Dilemaa????  RSS feed

 
Kumar Varun
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the code

class String
{
private int x;
String(int i)
{
x=i;
}
public int getX()
{
return x;
}
}

public class TryString
{
public static void main(java.lang.String[] arg)
{
String s = new String(5);
System.out.println(s.getX()+ " is output");
}
}


its working fine


String is a immutable Final class???

then wats its advantage f being immutable???
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use your imagination. You have not changed java.lang.String in any way whatsoever. You have defined a class called String in the default package. It is the way that packages are searched for classes that is causing the effects that you observe, it's nothing to do with the immutability of java.lang.String.

I notice that you have already found out that you must use java.lang.String in your main method otherwise you cannot run your program.
[ October 10, 2006: Message edited by: Barry Gaunt ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!