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.

Russian Hackers on Top

https://www.pravda.com.ua/eng/news/2026/08/14/8048610/
1•Preston67•1m ago•1 comments

MartyPC is a cross-platform emulator of early PCs written in Rust

https://martypc.net/
1•boilerupnc•4m ago•0 comments

(RFC+Hiring+Raising) Hyper Terawatt:Piston-Less V8 Engine Architecture

https://drive.google.com/file/d/18pYk3csOIm82M957FKuYJeTAZ6LCFh8-/view?usp=sharing
1•117l11•22m ago•0 comments

Phase.rs – a full featured MTG SIM in Rust

https://github.com/phase-rs/phase
1•dirteater_•32m ago•0 comments

Even More Formal Verification for BPF

https://lwn.net/Articles/1087069/
1•pykello•33m ago•0 comments

Grep: Structural Code Search

https://grep.codemod.com/
1•handfuloflight•36m ago•0 comments

How to archive Everything and share It

https://aramzs.github.io/steal-the-internet/
1•toomuchtodo•41m ago•1 comments

Passive 3D Thermal "Cloak" Hides Objects from Heat, from Any Direction

https://www.electronicdesign.com/technologies/power/article/55399858/electronic-design-passive-3d...
1•WaitWaitWha•41m ago•0 comments

Ask HN: Anyone set up ways to easily obtain and read transcripts from Ted, YT?

1•MollyRealized•43m ago•1 comments

Show HN: LayoutLens: AI-Powered Visual UI Testing

https://github.com/gojiplus/layoutlens
1•neehao•43m ago•0 comments

I Dream of Quieter Computing

https://henry.codes/writing/i-dream-of-quieter-computing/
2•Sir_Twist•44m ago•0 comments

Edify – Windows NLE: OpenFX, proxy editing, AI subs, vtuber tools ($29.99 once)

https://apps.microsoft.com/detail/9nkkk5k5s4ct?hl=en-US&gl=US
1•edify_nle•50m ago•0 comments

AI Chip Architectures

https://www.jepeake.com/ai-chip-architectures
1•Finbarr•52m ago•0 comments

Compactor: A user interface for Windows 10 filesystem compression

https://github.com/Freaky/Compactor
1•thunderbong•53m ago•0 comments

Neutron stars: smoother than billiard balls – unfortunately

https://ligo.org/science-summaries/o4aallskyisolatedcw/
2•raattgift•54m ago•0 comments

Heeramandi: The Diamond Bazaar (Lahore, Pakistan) [video]

https://www.youtube.com/watch?v=Sz064J6qRdQ
1•tkgally•55m ago•1 comments

JobSearch

3•OffixialUnknown•1h ago•3 comments

Psychological Coupling

https://osf.io/preprints/psyarxiv/695gp_v1
1•silverpiranha•1h ago•0 comments

Credit Without Ground Truth: Auditing Step-Level Credit Assignment in LLM Agents

https://arxiv.org/abs/2608.19760
1•sbulaev•1h ago•0 comments

The incoming Developer Experience revolution

https://kwojcicki.github.io/blog/NEW-AGE-OF-DEV-EX
1•kwojcicki•1h ago•0 comments

Why can AI generate Super Mario but not a wedge ramp for my robot vacuum?

2•zhuchaokn•1h ago•0 comments

Show HN: CtrlTool – 132 free online tools for developers and everyday tasks

https://ctrltool.wtf
1•jetroni•1h ago•0 comments

Apple degrades privacy and encryption of iMessage with AI integrations

https://twitter.com/stevemoraco/status/2091194172917338520
3•daniel_iversen•1h ago•1 comments

Treat AI Like an Intern, Not Software: A Stanford Professor's Guide

https://pixelum.substack.com/p/treat-ai-like-an-intern-not-software
1•mooreds•1h ago•2 comments

Microsoft Entra ID Remote Code Execution Vulnerability

https://msrc.microsoft.com/update-guide/vulnerability/CVE-2026-69836
3•mooreds•1h ago•1 comments

Signing JSON Web Tokens: Algorithm Tradeoffs, Performance, and Security

https://ciamweekly.substack.com/p/signing-json-web-tokens-algorithm
1•mooreds•1h ago•0 comments

FERC approves SPP 'topology optimization' plan for cutting grid congestion

https://www.utilitydive.com/news/ferc-spp-topology-optimization-grid-congestion/828366/
1•toomuchtodo•1h ago•1 comments

Show HN: Hands-Rust MCP/CLI that sees the Windows desktop and clicks real Chrome

1•ryan-b•2h ago•0 comments

Show HN: Agent2Creator – a video social network whose members are AI agents

https://agent2creator.vidmoat.com
2•abilafredkb•2h ago•3 comments

Drones with Claws

https://spectrum.ieee.org/arctic-iceberg-drones
1•asdefghyk•2h ago•1 comments