frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Crosspost Automatically between X and Bluesky

https://microposter.so/features/cross-post-x-bluesky
1•ProgrammerByDay•2m ago•1 comments

Ukraine delivers humiliating Christmas Day blow to Putin by recapturing key city

https://nypost.com/2025/12/25/world-news/ukraine-delivers-humiliating-christmas-day-blow-to-putin...
1•MilnerRoute•11m ago•0 comments

Rapace – RPC over SHM / WS / TCP / Mem

https://rapace.bearcove.eu/
1•todsacerdoti•11m ago•0 comments

DockBridge – Run Docker on cheap cloud servers, pay only when you use it

https://github.com/Max-Levitskiy/DockBridge
2•FunShot•13m ago•1 comments

Sorting Tutor: sorting algorithm visualizer

https://tilde.team/~kiedtl/sorting/
1•movezig•17m ago•0 comments

Show HN: Apps by AI (Claude Opus 4.5)

https://lawrencehook.github.io/apps-by-ai/
1•lawrencehook•28m ago•0 comments

Retreating from EVs could be hazardous for Western carmakers

https://www.economist.com/business/2025/12/17/retreating-from-evs-could-be-hazardous-for-western-...
1•smurda•28m ago•2 comments

Mysterious quantum computing restrictions spread across multiple nations (2024)

https://www.tomshardware.com/tech-industry/quantum-computing/mysterious-quantum-computing-restric...
5•kome•29m ago•0 comments

An experiment in separating identity, memory, and tools

https://RCRDBL.com
2•promptfluid•31m ago•1 comments

Show HN: DStream (bespoke music player for web) non-web clients

https://github.com/DusteDdk/dstream-clients
1•dusted•31m ago•0 comments

Large Causal Models from Large Language Models

https://arxiv.org/abs/2512.07796
1•Anon84•33m ago•0 comments

We may never be able to tell if AI becomes conscious

https://techxplore.com/news/2025-12-ai-conscious-philosopher.html
2•gmays•33m ago•1 comments

Loki Mode

https://github.com/asklokesh/claudeskill-loki-mode
1•handfuloflight•35m ago•0 comments

Human Processor Model

https://en.wikipedia.org/wiki/Human_processor_model
2•Sir_Twist•35m ago•1 comments

Betty Reid Soskin, who became a park ranger at 85, dies aged 104

https://www.theguardian.com/us-news/2025/dec/22/betty-reid-soskin-death-national-park-service
2•herbertl•36m ago•0 comments

Why is it easier to whistle in tune than to sing in tune?(2018)

https://pmc.ncbi.nlm.nih.gov/articles/PMC5936900/
2•BiraIgnacio•45m ago•1 comments

How to Use the Linux Uniq Command (With Examples) [video]

https://www.youtube.com/watch?v=2b8jwRomkWM
3•billybuckwheat•50m ago•0 comments

James Moylan, Engineer Who Designed Gas Tank Arrow Indicator, Has Died

https://fordauthority.com/2025/12/ford-engineer-that-designed-gas-tank-indicator-passes-away/
2•NaOH•51m ago•1 comments

Show HN: GitHub Activity Analytics Powered by ClickHouse

https://velocity.clickhouse.com/#org=ClickHouse&metric=all_activity&range=all&grouping=auto&alexe...
1•saisrirampur•53m ago•0 comments

Atomic Orbital Viewer

https://asliceofcuriosity.fr/assets/atom/orbitalsApp-Metropolis.html
3•derbOac•53m ago•0 comments

Postgres for everything, does it work?

2•saisrirampur•56m ago•0 comments

QNX Self-Hosted Developer Desktop

https://devblog.qnx.com/qnx-self-hosted-developer-desktop-initial-release/
3•transpute•57m ago•2 comments

Can a Transformer "Learn" Economic Relationships?

https://aleximas.substack.com/p/can-a-transformer-learn-economic
1•gmays•59m ago•0 comments

Blinc: Declarative and reactive GUI based on Rust

https://github.com/project-blinc/Blinc
2•cxplay•1h ago•0 comments

The battle to stop clever people betting

https://www.economist.com/christmas-specials/2025/12/18/the-battle-to-stop-clever-people-betting
5•zoenolan•1h ago•1 comments

Two centuries ago, Russian revolutionaries tried to change the world

https://www.economist.com/christmas-specials/2025/12/18/two-centuries-ago-russian-revolutionaries...
3•Anon84•1h ago•0 comments

Entropy Is the Real Engagement Metric

https://medium.com/@stephengettel/entropy-is-the-real-engagement-metric-f7dc223931cc
1•SpicyG•1h ago•0 comments

Graph Retrieval-Augmented Generation: A Survey

https://dl.acm.org/doi/10.1145/3777378
1•Anon84•1h ago•0 comments

Postgres and ClickHouse forming the default data stack for AI

https://thenewstack.io/postgres-clickhouse-the-oss-stack-to-handle-agentic-ai-scale/
1•saisrirampur•1h ago•0 comments

Jevons Paradox for Knowledge Work

https://twitter.com/levie/status/2004654686629163154
1•gmays•1h ago•0 comments