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.

Et AI.: A proposal for AI attribution

https://anagogistis.com/posts/et-ai/
1•anagogistis•56s ago•0 comments

PhD Admission 2026: Dates, Eligibility, Entrance Exams and Complete Guide

https://sites.google.com/view/backlinklistforcrawling
1•aimlay•1m ago•1 comments

Pseudorandom black swans: cache attacks on CTR_DRBG

https://security.cohney.info/blackswans/
1•fanf2•2m ago•0 comments

Gamified TLDR on A2A

https://mcpa2a.lovable.app
1•maieuticagent•4m ago•0 comments

People in Brazil are living past 110 and scientists want to know why

https://www.sciencedaily.com/releases/2026/01/260107225527.htm
1•phyzix5761•5m ago•0 comments

Show HN: The dev dashboard I built for my non-technical co-founder

1•akhnid•6m ago•0 comments

GM to take $7.1B hit from electric vehicle production changes, China

https://www.freep.com/story/money/cars/general-motors/2026/01/08/gm-to-take-7-1b-hit-from-ev-prod...
1•cebert•7m ago•0 comments

Show HN: Woid – High-performance C++ type erasure and polymorphism library

https://github.com/akopich/woid
1•akopich•9m ago•1 comments

Show HN: Rankiwiki a multilingual community ranking site

1•rankiwiki•10m ago•0 comments

Show HN: VeridisQuo – open-source deepfake detector with explainable AI

https://github.com/VeridisQuo-orga/VeridisQuo
1•theocastillo•11m ago•1 comments

Friday Links #33 – Fresh JavaScript Tools and Trends

https://jsdevspace.substack.com/p/friday-links-33-fresh-javascript
1•javatuts•12m ago•0 comments

Moss-kernel: a Linux-compatible kernel written in Rust

https://github.com/hexagonal-sun/moss-kernel
1•ravenical•16m ago•0 comments

A Simulation of Being Dropped Randomly in the Ocean Every Day for 5 Years

https://old.reddit.com/r/theydidthemath/comments/1q840uk/self_a_simulation_of_being_dropped_rando...
1•debesyla•17m ago•0 comments

Looking Back at the Best Inventions of 2001

https://shkspr.mobi/blog/2026/01/looking-back-at-the-best-inventions-of-2001/
2•blenderob•17m ago•0 comments

Organ Meat Is All the Rage Thanks to MAHA and the Natural Food Fad

https://www.bloomberg.com/news/articles/2026-01-09/liver-heart-and-tallow-are-maha-favorites-foun...
2•helsinkiandrew•18m ago•2 comments

Transcript: Are martial arts the answer to AI? – Yuval Noah Harari

https://www.danielfalbo.com/bookmarks/martial-arts-ai
2•danielfalbo•18m ago•0 comments

No 10: Grok changes 'insulting' and make deepfake creation a 'premium service'

https://uk.finance.yahoo.com/news/no-10-grok-changes-insulting-121140676.html
1•chrisjj•20m ago•1 comments

TuneKit: Fine-Tune SLMs

https://tunekit.app/
1•handfuloflight•20m ago•0 comments

39c3: In-house electronics manufacturing from scratch: How hard can it be? [video]

https://media.ccc.de/v/39c3-in-house-electronics-manufacturing-from-scratch-how-hard-can-it-be
1•fried-gluttony•21m ago•0 comments

TicTacToe for the AGC

https://github.com/NeilFraser/AGC-code/blob/main/Apps/TicTacToe.agc
1•ColinWright•21m ago•0 comments

Casio AE1200WH-1A

https://www.casio.com/us/watches/casio/product.AE-1200WH-1AV/
1•geowalker•22m ago•1 comments

China's humanoid robots come out fighting

https://www.ft.com/content/46d5a159-f6e5-4fd3-a08b-e58dd83ca0b1
1•ashishgupta2209•22m ago•0 comments

Elixir v1.19.5 Released

https://elixirforum.com/t/elixir-v1-19-5-released/73923
2•amalinovic•23m ago•0 comments

The Future of Coding Agents

https://steve-yegge.medium.com/the-future-of-coding-agents-e9451a84207c
2•terryf•23m ago•0 comments

SimpleMem: Efficient Lifelong Memory for LLM Agents

https://github.com/aiming-lab/SimpleMem
1•simonpure•23m ago•0 comments

AI LeetCode Tutor Platform Looking for Beta Users

https://codeboss.codes
1•ubervincet•24m ago•1 comments

The disappointingly ongoing success of WvDial (2009)

https://apenwarr.ca/log/20091224
2•pantalaimon•30m ago•0 comments

Why AI is both the problem and the cure for legacy code

https://leaddev.com/ai/why-ai-both-problem-cure-legacy-code
1•PretzelFisch•30m ago•0 comments

Telegram introduced AI Summaries. I filed bug report for them

https://bugs.telegram.org/c/58400
1•listic•31m ago•2 comments

Chinese Demand for Copper Vanishes After Prices Hit Record

https://finance.yahoo.com/news/chinese-demand-copper-vanishes-prices-075401948.html
2•thomassmith65•31m ago•0 comments