frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•10mo 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•10mo 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•10mo 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.

Hitachi Ltd, Part I

https://www.abortretry.fail/p/hitachi-ltd-part-i
1•XzetaU8•1m ago•0 comments

France pulls last gold held in US for $15B gain

https://www.mining.com/france-pulls-last-gold-held-in-us-for-15b-gain/
1•teleforce•2m ago•0 comments

A Tale of AI Betrayal and Snapshotting Victory

https://makc.co/essays/gpt-clusterfuck/
1•makc•3m ago•0 comments

Dennett's Real Patterns in Science and Nature

https://direct.mit.edu/books/oa-edited-volume/6112/Dennett-s-Real-Patterns-in-Science-and-Nature
1•__patchbit__•3m ago•0 comments

Microsoft Silverlight

https://en.wikipedia.org/wiki/Microsoft_Silverlight
1•tosh•7m ago•0 comments

The Image Boards of Hayao Miyazaki

https://animationobsessive.substack.com/p/the-image-boards-of-hayao-miyazaki
1•vinhnx•9m ago•0 comments

Show HN: Mantyx – Agent Control Plane with Centralized MCP Management

https://mantyx.io/mcp
2•mantyx•11m ago•0 comments

GPU Memory for LLM Inference (Part 1)

https://darshanfofadiya.com/llm-inference/gpu-memory.html
3•subset•15m ago•0 comments

The Population Bomb (1968)

https://en.wikipedia.org/wiki/The_Population_Bomb
2•downboots•24m ago•0 comments

Show HN: WebGPU LLM inference comprehensive benchmark

https://arxiv.org/abs/2604.02344
2•yu3zhou4•27m ago•0 comments

Show HN: Let's Turn Your Claude into Socrates

https://github.com/RoundTable02/socrates-skill
2•bevibing•44m ago•0 comments

FluxFest Season 1 kick off soon

1•loveTech•47m ago•3 comments

Show HN: Cloclo – open-source multi-agent CLI runtime for 13 LLM providers

https://www.npmjs.com/package/cloclo
3•seifbenayed1992•50m ago•0 comments

The Six Dumbest Ideas in Computer Security (2005)

https://www.ranum.com/security/computer_security/editorials/dumb/
1•Tomte•54m ago•0 comments

ELF and Dynamic Linking

https://fmdlc.github.io/tty0/Linux_ELF_Dynamic_linking_EN.html
1•signa11•59m ago•0 comments

Estimates of the expected utility gain of AI Safety Research

https://www.lesswrong.com/posts/gXYeWoAfSrdGogchp/estimates-of-the-expected-utility-gain-of-ai-sa...
1•joozio•1h ago•0 comments

The Only Privately Titled EV1 Is Getting Restored with GM's Help

https://www.caranddriver.com/news/a70929844/gm-helping-restore-only-privately-titled-ev1/
2•greesil•1h ago•0 comments

SIMD in Go

https://programmerscareer.com/golang-simd/
1•djood•1h ago•0 comments

It has never been about code

https://www.ufried.com/blog/never_been_about_code/
1•BinaryIgor•1h ago•0 comments

SideX – A Tauri-based port of Visual Studio Code

https://github.com/Sidenai/sidex
46•0x1997•1h ago•33 comments

Shadowing a British Blood Biker on Shift [video]

https://www.youtube.com/watch?v=N-lJbkNSeO8
1•CaliforniaKarl•1h ago•0 comments

Man quit tech job after 60 to become professional poker player

https://www.theguardian.com/lifeandstyle/2026/apr/06/a-new-start-after-60-i-jacked-in-my-job-in-t...
2•erehweb•1h ago•0 comments

Have the patents for H.264 MPEG-4 AVC expired yet?

https://meta.wikimedia.org/wiki/Have_the_patents_for_H.264_MPEG-4_AVC_expired_yet%3F
3•airstrike•1h ago•0 comments

LinkedRecords is now MIT-licensed

https://github.com/wolfoo2931/linkedrecords
2•WolfOliver•1h ago•0 comments

Termux: Differences from Linux

https://wiki.termux.com/wiki/Differences_from_Linux
3•thunderbong•1h ago•0 comments

China fell for a lobster: What an AI assistant tells us about Beijing's ambition

https://www.bbc.com/news/articles/cy41n17e23go
8•tmoravec•1h ago•0 comments

Rawfeed.social

https://github.com/ozziest/rawfeed.social
2•iozguradem•1h ago•1 comments

We cut our agent's API costs by 10x with prompt caching

https://kern-ai.com/blog/prompt-caching
2•obilgic•1h ago•0 comments

A new regional order for the Strait of Hormuz

https://www.aljazeera.com/opinions/2026/4/6/a-new-regional-order-for-the-strait-of-hormuz
2•defrost•1h ago•0 comments

Overwrite This Website

https://overwritten.site
1•nextyes•1h ago•1 comments