• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use Of String Constants  RSS feed

 
Arvind Sundaram Jesudasan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Geeks, I have a qn about String constants.
Why do we need to use String constants? (ie. public static final String xxxx = "xxxxx";)
I understand, we can reuse it if we group the constants in a class or where ever we declare.
Also java uses the strings in a constant pool and it will reuse it if we request for the same string.

Is there any other facts that i'm missing?

The reason is, i am using test package in my project and test classes use lot of strings ( Just to mock the data to my test methods). When i use Sonar, it complains about duplicate literals.
I can declare all of them in a constant file. but in my opinion that would be convoluted.

Please share your thoughts.

Thanks,
Arvind
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One reason is if you want to change the string. If you have the same string literal in many places in your code, it will be a lot of work to change it everywhere. If you define a constant and use that, then you need to change it only in one place.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you will have to recompile all classes that use the constant anyway, because compile time constants (such as Strings) have their values inserted into byte code, not the references.
 
Arvind Sundaram Jesudasan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fair enough. Is there anything we need to consider?
Especially in my scenario, do i need to move the test package constants into a string constant file? Too many mock'ed objects and strings used. if i move them would it be a good design?
Please share your thoughts on this as well.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arvind Sj wrote:Fair enough. Is there anything we need to consider?
Especially in my scenario, do i need to move the test package constants into a string constant file? Too many mock'ed objects and strings used. if i move them would it be a good design?

Well, in answer to your first (and, I suspect, main) question, the thing you need to ask yourself is: Are these "things" that you're trying to 'mock' actually Strings?

You might want to read the StringsAreBad page, because very often they're NOT.

HIH

Winston
 
Arvind Sundaram Jesudasan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your views. I will put my thoughts on this and come up with my questions if have any.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!