frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

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.

Archive-It – Built at Archive.org

https://archive-it.org/
1•gurjeet•4m ago•0 comments

Show HN: Camera Lets You See Sound in Complete Darkness [video]

https://www.youtube.com/watch?v=1zeHfH5TzWY
1•Regina0727•7m ago•0 comments

Grid-Forming vs. Grid-Following Inverters

https://www.batterydesign.net/grid-forming-vs-grid-following-inverters/
1•leonidasrup•8m ago•0 comments

UC Berkeley Law Artificial Intelligence Policy

https://www.law.berkeley.edu/academics/registrar/academic-rules/artificial-intelligence-policy/
1•fbrusch•9m ago•0 comments

The World Cup Is a Petri Dish

https://www.bloomberg.com/news/articles/2026-05-28/how-companies-are-tracking-disease-threats-dur...
1•petethomas•10m ago•0 comments

Show HN: BetterCallClaude – Open Source AI Legal Agents for Italy

https://bettercallclaude.it/
1•ReDeiPirati•13m ago•0 comments

Emacs Bra Size Calculator

https://pulusound.fi/blog/emacs-bra-size-calculator/
1•grumblepeet•16m ago•0 comments

MTerm

https://github.com/d0x2a/mTerm
1•vad_n__•16m ago•0 comments

WP23

https://wordpress.org/news/2026/05/wp23/
1•OuterVale•19m ago•0 comments

I Found the US Nuclear Detection System in Space [video]

https://www.youtube.com/watch?v=DjLnIb41DuQ
2•noazark•21m ago•0 comments

Mini-SWE-agent scores up to 74% on SWE-bench in 100 lines of Python code

https://mini-swe-agent.com/latest/
1•fittingopposite•23m ago•0 comments

A Comprehensive Analysis of the Open-Weight Frontier (May 2026)

https://deepresearch.ninja/2026/05/Qwen3.6-27B/35B-A3B-vs-Gemma-4-vs-DeepSeek-V4-A-Comprehensive-...
1•jackalxyz•31m ago•0 comments

PeerDrop – Direct file transfer. No cloud

https://peerdrop.app/
1•janandonly•32m ago•0 comments

Perfect randomness realized for the first time

https://phys.org/news/2026-05-randomness.html
2•arunc•32m ago•0 comments

The world's carmakers are struggling to compete with China

https://www.bbc.com/news/articles/c4g8vg72z43o
1•nairteashop•35m ago•1 comments

NovaGPU – An open-source GPU architecture written in Verilog

https://github.com/nova-studios-hw/novagpu-ts1t
3•novastudios-hw•36m ago•0 comments

Ask HN: How Much a Difference Does an LLM's Corpus Make?

1•giardini•36m ago•0 comments

Ask HN: Why not have an EU browser?

2•osigurdson•36m ago•3 comments

Oilfield units: a measurement system so cursed it made me change career [video]

https://www.youtube.com/watch?v=sdWEGzWFcCc
1•dgellow•38m ago•0 comments

Clojure → YAMLScript transpiler: using LLMs for normalization, SCI for execution

https://github.com/whitecell-dev/cljys
1•MaykonMan•39m ago•0 comments

Unemployment Simulator 2018

https://store.steampowered.com/app/3252360/Unemployment_Simulator_2018/
2•MrBuddyCasino•40m ago•0 comments

DOJ charges Google staffer over Polymarket trades netting $1.2M

https://text.npr.org/nx-s1-5836659
1•hvb2•40m ago•0 comments

Show HN: Product Trailers – The TV Channel for Product Hunt Launches

https://producttrailers.xyz
2•kentich•41m ago•0 comments

SpaceX: Did Elon Musk Just Rig the Stock Market? [video]

https://www.youtube.com/watch?v=sYA-z0Y8WRQ
6•mgh2•47m ago•0 comments

Show HN: Pre-computed market context for agents

https://tickerdb.com/
1•wolfman1•50m ago•0 comments

Sigbovik 2026 Proceedings [pdf]

https://sigbovik.org/2026/proceedings.pdf
4•boodleboodle•56m ago•0 comments

JAL to launch Moon payload service, aims to preserve heritage

https://english.kyodonews.net/articles/-/76763
2•anigbrowl•57m ago•0 comments

Amdahl's law for AI agents

https://electric.ax/blog/2026/02/19/amdahls-law-for-ai-agents
1•pramodbiligiri•58m ago•0 comments

Class CrunchLabs

https://www.classcrunchlabs.org/
3•mgarciaisaia•1h ago•0 comments

Parallelizing Arbitrary Python Code by Running 1M Python Interpreters on a GPU

https://github.com/jndean/gpusnek
1•jffry•1h ago•0 comments