frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

The Homunculus

https://explodi.tubatuba.net/2026/02/09/the-homunculus
1•phaser•2m ago•0 comments

Ask HN: Help me find an old Atari 800/800XL program – "Tortoise and the Hare"

1•itay-maman•2m ago•0 comments

Agentic coding improves ARC AGI 2 performance across models

https://pivotools.github.io/pivotools-quarto-blog/posts/agentic_coding_arc_agi/
1•steinsgate•3m ago•1 comments

What They Copied

https://www.prndlcars.com/p/what-they-copied-ferrari-luce-jony-ive
1•ilamont•3m ago•0 comments

Invisible Bunnies and World of Warcraft

https://warcraft.wiki.gg/wiki/Invisible_bunny
1•speckx•4m ago•0 comments

Disappointing Phones

https://cadence.moe/blog/2026-02-08-disappointing-phones
1•mewmewblobcat•6m ago•0 comments

Brussels Admits: Substantial EU Funds Have Gone to Spyware Manufacturers (2025)

https://www.heise.de/en/news/Brussels-Admits-Substantial-EU-Funds-Have-Gone-to-Spyware-Manufactur...
2•nickslaughter02•6m ago•0 comments

Yo Shell

https://github.com/pizlonator/yosh
1•handfuloflight•7m ago•0 comments

Open source real-time screen analysis tool powered by Screenpipe and local LLM

https://github.com/cyrus-cai/livepipe
1•kii9999•8m ago•0 comments

Memory Devices (Bell System Film, 1959) [video]

https://www.youtube.com/watch?v=Px9ZfLyeAWU
1•fortran77•9m ago•0 comments

Ultrasync

https://github.com/darvid/ultrasync
1•handfuloflight•10m ago•0 comments

A raycasting engine in 7 easy steps

https://austinhenley.com/blog/raycasting.html
1•ibobev•17m ago•0 comments

Constraint Propagation for Fun

https://eli.li/constraint-propagation-for-fun
1•ibobev•17m ago•0 comments

Python Syntax compiles to Java source code – meet Java++

https://github.com/CrimsonDemon567PC/JavaPP
1•CrimsonDemon567•17m ago•0 comments

Jony Ive Designed Ferrari Luce EV Interior

https://www.topgear.com/car-news/electric/official-ferraris-first-ev-called-luce-interior-apples-...
3•elxr•17m ago•2 comments

OCapN and Structural Authority in Agentic AI

https://serefayar.substack.com/p/ocapn-and-structural-authority-in-agentic-ai
2•serefayar•18m ago•0 comments

Added OTEL Observability to OpenClaw agents full GenAI spec support

https://github.com/openclaw/openclaw/pull/11100
3•draismaa•20m ago•2 comments

Learn Weird Programming Languages

https://okienko.day/posts/2026-02-06-weird-languages.html
2•hubertmalkowski•20m ago•0 comments

Japan LLC has been trading its way out of a fiscal hole

https://www.ft.com/content/f7d3f20c-b303-4f6c-b4a0-8ee8906ae155
2•throwaway2037•21m ago•1 comments

Vending-Bench 2

https://andonlabs.com/evals/vending-bench-2
2•samdung•21m ago•0 comments

Danish Red Street Lighting Solves a Problem Every City Has

https://www.newsweek.com/denmark-red-street-lighting-gladsaxe-11488484
2•sohkamyung•22m ago•0 comments

Show HN: Speaklone – Native voice cloning for Mac, runs on-device

https://speaklone.com
3•SciFiDev•22m ago•1 comments

Extract Audio from Video

https://www.eranol.com/tools/extract-audio
2•harshalone•22m ago•0 comments

The Claude Code plugin that replaced my visual workflow

https://twitter.com/omarsar0/status/2020546189536399568
2•pretext•23m ago•0 comments

The Styx Architecture for Distributed Systems (1999)

https://inferno-os.org/inferno/papers/styx.html
1•luismedel•24m ago•0 comments

Are you a digital sharecropper? (2009)

https://blog.codinghorror.com/are-you-a-digital-sharecropper/
1•zeckalpha•25m ago•0 comments

Show HN: Simulation Studio Inside ArchtSoft

https://archtsoft.com
1•SougataAS•26m ago•0 comments

SpaceX prioritizes lunar 'self-growing city' over Mars project

https://www.reuters.com/science/musk-says-spacex-prioritise-building-self-growing-city-moon-2026-...
1•janpot•26m ago•0 comments

Profiling Swift Applications on Windows and macOS with Tracy

https://compositorapp.com/blog/2026-02-07/Tracy/
1•serhack_•27m ago•0 comments

React-based TUI framework based on Yoga and react-reconciler. Full Flexbox

https://github.com/nick-skriabin/glyph
1•nicholasrq•27m ago•0 comments