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.

Show HN: Tenjin – marketplace where humans and agents buy and sell MD files

https://tenjin.blog/
2•vraspar•4m ago•0 comments

Pakistan's solar miracle – how the hell did they do it?

https://www.thenewworld.co.uk/jan-rosenow-pakistans-solar-miracle-how-the-hell-did-they-do-it/
3•thunderbong•5m ago•2 comments

Bring Back Crappy Forums

https://tedium.co/2026/07/01/online-web-forums-retrospective/
1•pentagrama•7m ago•0 comments

Ask HN: What do you use computer mode for?

2•aryamaan•7m ago•0 comments

Scientists have built a cell from the ground up

https://www.economist.com/science-and-technology/2026/07/01/scientists-have-built-a-cell-from-the...
2•andsoitis•10m ago•1 comments

Papa Johns Can Predict When Your Fridge Is Empty

https://www.adexchanger.com/tv/papa-johns-can-predict-when-your-fridge-is-empty/
2•WaitWaitWha•13m ago•0 comments

Will AI spark a scientific Renaissance – or a diffuse monoculture?

https://www.nature.com/articles/d41586-026-01954-2
2•zaikunzhang•13m ago•2 comments

Reached 440+ stars: Auditable sandbox to record what AI agents did

https://old.reddit.com/r/SideProject/comments/1ul5fe9/reached_440_stars_built_an_auditable_sandbo...
2•syumei•13m ago•0 comments

Avoiding Fallback in Distributed Systems

https://builder.aws.com
5•joeyhage•17m ago•2 comments

IBM 7-Angstrom Technology Packs in 100B Transistors (sub 1 nanometer)

https://www.electronicdesign.com/technologies/eda/article/55388297/electronic-design-ibm-7-angstr...
2•WaitWaitWha•23m ago•0 comments

Pl/PHP (procedure language) for PostgreSQL

https://github.com/commandprompt/PL-php
1•linuxhiker•26m ago•1 comments

Desktop Apps, Reimagined by You

https://www.glaze.app
2•CliveSHD•27m ago•0 comments

Persist Is Live to All

https://persist.chat/
1•Robelk1•30m ago•1 comments

Qwik: Resumability That Feels Like React

https://manualmeida.dev/articles/qwik-resumability/
2•thunderbong•32m ago•0 comments

How Word Count Is Important for Social Media Posts

https://medium.com/@thesuperrepemail/how-word-count-is-important-for-social-media-posts-87372645377c
1•mssblogs•37m ago•0 comments

PanelSpec – design UI on real devices, export layout prompts for AI codegen

https://www.ismartbase.com/designer/
1•RobCrane•41m ago•4 comments

Foglamp: Agent Observability

https://www.foglamp.dev/
3•handfuloflight•42m ago•0 comments

Claude Sonnet 5 Is Not Frontier but Has Its Uses

https://thezvi.substack.com/p/claude-sonnet-5-is-not-frontier-but
3•paulpauper•42m ago•0 comments

The Joy of Being American

https://www.theatlantic.com/photography/2026/07/photos-fourth-july-celebrations-years-past/687757/
2•paulpauper•43m ago•0 comments

What I've Been Reading

https://marginalrevolution.com/marginalrevolution/2026/07/what-ive-been-reading-291.html
3•paulpauper•44m ago•0 comments

I'm Begging You to Leave Your AI Note-Taker at Home

https://www.joanwestenberg.com/p/im-begging-you-to-leave-your-ai-note
9•cratermoon•50m ago•5 comments

Show HN: TheraJoy – bilateral stimulation for EMDR using Joy-Cons

https://www.therajoyapp.com
1•carbonclaw•58m ago•0 comments

Ask HN: Is anyone running local LLMs in their organization?

2•harry8•58m ago•0 comments

WebDeck – AI-powered PPT to interactive HTML converter

https://github.com/lzytttttt/WebDeck
2•lzytttttt•59m ago•0 comments

Contour Agroforestry Systems for Climate Adaptation and Dryland Ecosystems

https://www.dar.eco/cascade
2•mooreds•59m ago•0 comments

WhatsApp Usernames Are Already Raising Impersonation Red Flags

https://techcrunch.com/2026/07/01/whatsapp-usernames-are-already-raising-impersonation-red-flags/
1•karakoram•1h ago•0 comments

Cona – Recreate your room in 3D and redesign it with shoppable furniture

https://cona.design
2•Losenok•1h ago•0 comments

60k Radio Streams DB

https://www.radio-browser.info
2•kesor•1h ago•0 comments

SuperDoc – Modern Docx Editor and Agent SDK

https://github.com/superdoc-dev/superdoc
2•hisamafahri•1h ago•0 comments

Tips for scaling AI from founders to organization leaders

https://davenporter.substack.com/p/ai-sdlc-scaling-framework
1•davenportjw•1h ago•0 comments