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.

Audience Capture

https://www.seangoedecke.com/audience-capture/
1•gfysfm•1m ago•0 comments

Show HN: Flick – A sports scores app without spoilers

https://flick.kawaicheung.io/appstart.html
1•kiwigod17•3m ago•0 comments

What Are the US Midterms

https://www.bbc.com/news/articles/c6wyz223j19po
2•teleforce•3m ago•0 comments

Reading Is Not Steering: Symbol-Readers Concept-Controllers in SAE's

https://github.com/CharacterCraftAI/Gemma-SAE-Research
1•justinhernandez•6m ago•0 comments

Salesforce Signs Definitive Agreement to Acquire Cresta

https://www.salesforce.com/news/stories/salesforce-signs-definitive-agreement-to-acquire-cresta/?...
1•johnbatch•7m ago•0 comments

My reflex of assuming our servers need ever-growing amounts of RAM

https://utcc.utoronto.ca/~cks/space/blog/sysadmin/ServerRAMGrowthReflex
1•birdculture•8m ago•0 comments

From Heroku to Convox, and why we built Rack Gateway

https://docspring.com/blog/posts/from-heroku-to-convox-and-rack-gateway/
1•nathan_f77•11m ago•0 comments

Police Dept. Quits After Finding Out What It Feels Like to Be Tracked

https://www.gadgetreview.com/tables-turned-entire-police-department-quits-after-finding-out-what-...
1•thisislife2•13m ago•0 comments

Show HN: Learn French by Watching Netflix

2•nshelia•16m ago•0 comments

Terralien

https://play.terralien.antari.io/
1•AndrewAnt•17m ago•0 comments

Ask HN: Does anyone have a contact with Cleveland-area Ham club?

2•stockresearcher•22m ago•0 comments

Show HN: Learn to speak French by watching Netflix

https://chromewebstore.google.com/detail/greetai-learn-speak-langu/hhildamloicfginbdnilakddipgkolbf
2•nshelia•22m ago•0 comments

Building a Private Telephone System [video]

https://www.youtube.com/watch?v=C4DY0NzaO14
2•jumpocelot•22m ago•0 comments

Intel stock collapse worsens as $3.5T chip rout spreads

https://finance.yahoo.com/markets/article/the-intel-stock-collapse-is-getting-worse--and-the-37-t...
8•entryleveljobsc•23m ago•0 comments

Dampe RNG has been solved for Ocarina of Time

https://twitter.com/zfg111/status/2116562542218932255
1•linksbro•23m ago•1 comments

An Existential Guide To: Flirting

https://theshadowedarchive.substack.com/p/an-existential-guide-to-flirting
1•jger15•25m ago•0 comments

Faulty AI face recognition is wrongly accusing alarming numbers of shoplifting

https://www.gadgetreview.com/the-number-of-innocent-people-being-accused-of-shoplifting-by-faulty...
1•entryleveljobsc•25m ago•0 comments

Au High Court to consider whether under-16s social media ban is unconstitutional

https://www.abc.net.au/news/2026-11-03/high-court-challenge-to-under-16s-social-media-ban/107336886
1•anenefan•28m ago•1 comments

3rd-party statistically-correct benchmarks comparing WebAssembly runtimes

https://wasm.fyi/benchmarks/
1•JairusSW•33m ago•1 comments

My Name Is Riemann – Prerelease

https://huggingface.co/OpenPeerAI/My-Name-is-Riemann-Prerelease
1•gamer456148•36m ago•0 comments

Anthropic's pay packages cost the company more than $100k a month per employee

https://www.businessinsider.com/anthropic-ipo-filing-q3-personnel-costs-2026-11
1•trgfdffd•37m ago•0 comments

Developers are downgrading their AI on purpose – and it's working

https://thenewstack.io/claude-code-developer-refusals/
2•noptd•38m ago•0 comments

One of Google's Android partners turned 1.3M set-top boxes into a botnet

https://www.theverge.com/1024807/google-botnet-android-tv-shut-down
3•donohoe•40m ago•0 comments

From Words to Waypoints: Teaching a Small LLM to Plan for Robots

https://www.daweisun.me/blog/stl-planning-rl/
1•sundw•40m ago•0 comments

An open-source tool that makes eye-candy Instagram posts that doesn't use AI

https://www.useinstatools.com/
1•nicholasxdavis•41m ago•1 comments

Scaling Git at OpenAI

https://openai.com/index/scaling-git-at-openai/
15•makeramen•43m ago•0 comments

Show HN: HN Books – the books Hacker News keeps recommending

https://www.hacker-books.io/
1•pmoxim•45m ago•0 comments

Spairally 's safety circles are here

https://apps.apple.com/us/app/spairally/id6769132339
1•wfreza99•45m ago•0 comments

Show HN: Hacker News, à la Lobsters

https://git-aarvin.github.io/hackers/
2•hn-aarvin•46m ago•0 comments

I checked whether my laptop is wasting RAM

https://ansnadeem.com/2026/11/02/is-my-laptop-wasting-ram/
1•ansnadeem•53m ago•1 comments