Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

static final variables  RSS feed

 
ajse ruku
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • 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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • 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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12529
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
prakash chandra
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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.
 
R van Vliet
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • 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.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37180
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Jeanne ... I meant the same thing
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!