• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static final variables

 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

There are some requirements in my application because of which i am creating lots of static final string variables in my class. I need to pick one of the variable based on if/else blocks. I can't use properties file for this as well. Please let me know if there is any better way compare to static final variables OR creating lots of static final string variables are fine if we consider performance etc? Can i use xml ?
Please suggest.

regards,
Ajse
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can i use xml ?


You can use a shovel to peel an apple. Why would you use xml ?
 
ajse ruku
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just to add there could be 25-30 variables per class.

That's what i want to know. If xml is better that having 30 static final variables in a class ? Otherwise any better way or static final variables are fine.
 
Master Rancher
Posts: 4831
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ajse ruku wrote:Hi all,

There are some requirements in my application


I think this conversation will be much more productive if you actually tell us the requirements, rather than letting us guess about them.

ajse ruku wrote:because of which i am creating lots of static final string variables in my class. I need to pick one of the variable based on if/else blocks.


I doubt that. What are your requirements, actually?

ajse ruku wrote:I can't use properties file for this as well.


I doubt that too. Who says that you can't? Are they paying you to not use a properties file? Is it a school assignment where the instructor has said "don't use a properties file"? Or what?

ajse ruku wrote:Please let me know if there is any better way compare to static final variables OR creating lots of static final string variables are fine if we consider performance etc?


Hm, I can't tell what you're trying to do here. I suggest you forget about performance, at first, and try to describe your problem more completely.

ajse ruku wrote:Can i use xml ?


Sure, feel free. So far, there's no reason to think it matters either way.
 
ajse ruku
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your views.

My question is - Can we have 30 static final strings in a class ? If yes then fine otherwise is there any better way ? Properties file is not into consideration.

regards,
Ajse
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

The answer to your question is yes.

Having 30 static final String variables will not give you any compiler errors.

I think the question you want to ask is "Is using 30 static final String objects suitable in my situation: <explain situation here>, or is there a better way?"

I like to use enums, but they are not suitable for all situations.

Sean
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ajse ruku wrote:
if yes then fine otherwise is there any better way ?


My guess is that even though the answer is 'yes', there is STILL a better way...but without knowing what you're doing, it's impossible to say.

Software design is almost always a balancing act. There is run-time speed vs. memory vs development time vs. cost of maintaining code vs project requirements vs probably about 80 other things.

Part of being a good coder is learning how to balance ALL of these. Without knowing more about your project, nobody can say what that better way would be.

For example: What is the best way to get from city 'a' to city 'b' depends on the distance, how fast you need to get there, what site seeing you may want to do along the way, what time of year, whether you own a car or not...

there isn't one single best answer that works for all situations...

 
ajse ruku
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Requirement is such that i need to have 30-35 static final strings (generally one line 90-100 words) having some variables.
At run time based on data available,i need to pick a string , replace the variables with values and then pass that to some other method which is not my concern.

regards,
Ajse
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use 30-35 static final variables in a class. But make sure you don't repeatedly use the same String literal in the values. You can collect all the String literals that are to be used repeatedly in values. Then declare a static final variable for that repeating literal and use the variable instead of literal. For e.g.


In the above code use of "config/" literal is avoided in CONFIG_PATH and CONFIG_FILE.
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ajse ruku wrote:Requirement is such that i need to have 30-35 static final strings (generally one line 90-100 words) having some variables.
At run time based on data available,i need to pick a string , replace the variables with values and then pass that to some other method which is not my concern.

regards,
Ajse



Couldn't you just make a final static string array and select the appropriate one at runtime? An enum with a string field would do as well and is probably even more elegant. Certainly beats hardcoded references to static strings. All that said hardcoding such strings into a class sounds like a suboptimal design choice to me.

And for future reference please include as much relevant information about the problem you're trying to solve in your original post. It's a little inefficient for everyone to have to ask about specific details just so they can help you with your problem.
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you have more than one class ... having the same static final string variables ...

you can create an interface and put all those variables in there ...

just implement the interface and you are done ... hope this works



happy coding ... Lucky
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lalit Mehra wrote:you can create an interface and put all those variables in there ...

just implement the interface and you are done ...


This is actually the "Constant Interface Anti-Pattern." In Java 5+, you can use a static import to import all the variables from the interface without resorting to implementing the interface.
 
Lalit Mehra
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Jeanne ... I meant the same thing
 
I AM MIGHTY! Especially when I hold this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic