frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•1y ago
I'm relatively new to Java. I often notice a pattern where there is a list of constant which value are equal to the name.

  class Constant {
    public static final String ALBUM = "album";
    public static final String NAME = "_name";
    public static final String DISPLAY_NAME = "display-name";
    public static final String SERVICE_NAME_METRIC_NAME_PREFIX = "service_name.metric_name";
  }

Here is a public example of this practice I could find: https://developer.android.com/reference/android/provider/MediaStore.MediaColumns

I could understand that this might help in 2 ways refactoring and typo. This reduces chance of typo because you'll get compile error instead of run-time error if you typo a constant. This might also help in refactoring if you ever wants to change the value. but if may use this android public API example, I don't think it's wise to change a field name ever. If it's decommissioned, it's good to keep it so we don't re-use the field. If it's a new better field available, I think it should have a different name. I maybe making a straw man argument here. Let me know. If it's an internal API where such refactoring might make sense -- I still kind of think internal API should also be backward compatible, replacing a string are not a complicated operation in my opinion.

I see that this practice has a cost. One being that in every class that use this API. You need to add an import. It's also often the const is only used once from my experience.

  import static com.example.MediaFields.NAME;
  import static com.example.MediaFields.DISPLAY_NAME;

  String value = json.getString(NAME);
  String value2 = json.getString(DISPLAY_NAME);
vs

  String value = json.getString("name");
  String value2 = json.getString("display_name");
You write 1 line for declaration plus 2 lines for each class using this API. This is not a big deal in terms of LoC and I'm not an LoC police. However, my sense is the cost outweigh the benefit.

What do you think?

Comments

lanna•1y ago
You just made TWO typos: "display-name" vs "display_name" and "_name" vs "name", automatically counter-argumenting your point.

It is also for documentation. With the declared constants, we know all possible values. With plain strings, how am I supposed to know which values to use?

The benefits far outweigh the marginal cost.

ramsicandra•1y ago
The -, _, and leading _ are just variations of white space / separator I have encountered. I think it's possible to document all the allowable values in the Javadoc section of the function that takes in string as their argument.

In the specific android example, I would put it here. Under projection params where it takes in all the Images.Media.* string consts.

https://developer.android.com/reference/android/content/Cont...

Though, if it's a practice of Java Engineer to document allowable enum like string as a constant, then I can say that's a valid argument.

A Great Deal of Information About Hacker News

https://vale.rocks/posts/hacker-news
1•OuterVale•2m ago•0 comments

Show HN: Jolta – Automatic per-project JDK switching (like Volta, for Java)

https://oneappplatform.github.io/jolta/
1•pdxdave•13m ago•0 comments

Ukraine' Simple But Effective Protection Against Russian Starlink Jamming (2023)

https://www.technology.org/2023/08/01/did-russia-learn-to-jam-starlink-satellites-ukraine-is-alre...
1•pinkmuffinere•15m ago•0 comments

Exercise Erased Half the Molecular Signature of Muscle Aging

https://www.gethealthspan.com/research/article/exercise-molecular-signature-muscle-aging?zp_type=...
1•bilsbie•20m ago•0 comments

Evaluation of a pentapropellant upper stage (1970) [pdf]

https://ntrs.nasa.gov/api/citations/19700022572/downloads/19700022572.pdf
1•Eridanus2•21m ago•0 comments

Show HN: Modelfac

1•IceRay•28m ago•1 comments

Wine 11.14 – Run Windows Applications on Linux, BSD, Solaris and macOS

https://www.winehq.org/announce/11.14
1•neustradamus•28m ago•0 comments

Show HN: git clone https://git.swerdlow.dev

https://git.swerdlow.dev/
2•benswerd•33m ago•2 comments

Creative Focus

https://herbertlui.net/creative-focus/
4•herbertl•40m ago•0 comments

OpenAI did not notice Hugging Face hack for a week

https://www.reuters.com/business/its-ai-agent-spent-days-hacking-company-sources-say-openai-did-n...
5•himaraya•49m ago•1 comments

The SharePoint zero-day panic this week is a marketing event

https://tensorust-site.vercel.app/
1•Shmungus•50m ago•0 comments

The computer has been surprising us

https://iwhalen.com/computer-surprise/
3•iwhalen•52m ago•0 comments

Claude used my pipeline to find a counterexample to the Jacobian conjecture

3•JGPTechCo•58m ago•1 comments

The final frontier – 01005 passives (2012)

http://siliconexposed.blogspot.com/2012/06/final-frontier-01005-passives.html
1•gregsadetsky•1h ago•0 comments

The Shape of Apps

https://parakeet.co/blog/the-shape-of-apps/
3•wpm•1h ago•0 comments

Bonsai – a toolkit for building AI chat apps with branchable conversations

https://github.com/Joaoha/Bonsai
2•joaoha•1h ago•0 comments

In desperate need of money so im willing to make anything

1•lowke•1h ago•1 comments

Ask HN: How would I restore this 101 year old postcard?

2•Waterluvian•1h ago•1 comments

Project Babylon: Gerald Bull's Downfall (2006)

https://www.damninteresting.com/project-babylon-gerald-bulls-downfall/
3•networked•1h ago•0 comments

Stripe closed my account with a 0.18% dispute rate

6•rellyattd•1h ago•3 comments

What Happened to WordPerfect? Why the Dominant Word Processor Disappeared

https://www.youtube.com/watch?v=cMnjphv3DQo
4•cable2600•1h ago•1 comments

Show HN: Made a Free Shownotes Generator Website

https://shownotesgenerator.com/
1•wbemaker•1h ago•2 comments

Show HN: OneWayInterview – type a job role, AI builds the async video interview

https://onewayinterview.com/
1•emunova•1h ago•1 comments

LoRA Adapters Generated from Skill.MDs

https://terradev.cloud/tessera
1•Facingsouth•1h ago•0 comments

Open Weights and American AI Leadership

https://www.microsoft.com/en-us/corporate-responsibility/topics/open-weight/
7•haritha1313•1h ago•2 comments

BackSearch – Letting agents search the web

https://www.gr.inc/releases/introducing-backsearch
1•haritha1313•1h ago•0 comments

Cultural moderation of demographic differences in narcissism

https://www.tandfonline.com/doi/full/10.1080/15298868.2025.2593298#abstract
3•bushwart•1h ago•0 comments

Show HN: I rebuilt Microsoft Comic Chat's layout engine in one HTML file

https://arcade.pirillo.com/comic-chatter.html
2•ChrisPirillo•1h ago•1 comments

Amazon cracks down on use of AI images by sellers after New York law

https://www.cnbc.com/2026/07/23/amazon-makes-sellers-label-ai-generated-people-in-images-after-ny...
8•baranul•1h ago•0 comments

Ask HN: Multi dimensional sort (beyond Hilbert curve)

1•adec314•1h ago•0 comments