6

Is there a class that has defined constants for common port numbers like 80, 443, 21?

The only way right now I found is using getDefaultPort on URL that has a http, https or ftp schema, but it looks excessive to just get a constant.

I'm asking because I don't want to throw a bunch of magic numbers around the code and I assume there should be a place with those constants somewhere (e.g. we have one for UTF8 now).

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
  • Including common libraries or just pure JDK? – Impulse The Fox Apr 19 '18 at 20:30
  • Question: why do you want to know? Just to listen on a port? – markspace Apr 19 '18 at 20:33
  • Nope, there is none. – Antoniossss Apr 19 '18 at 20:34
  • Not exact duplicate, I'm asking about other ports also, not only HTTP and HTTPS. – Krzysztof Krasoń Apr 19 '18 at 20:35
  • 2
    I guess since these port numbers are well known, then it would be duplicating another source to make these constants in the Java API. By which I mean most people just look up the port number they need and define their own constant. It's not a bad thought to ask this question, but I think the Java API designers decided this was something best left to the organizations that already handle it. – markspace Apr 19 '18 at 20:38

1 Answers1

3

Not that I know of, but I am curious too. That would be nice. I know this is not an "answer," but in case you wanted to create your own, here is a table that I use (I would have put it in a comment, but I can't paste photos):

enter image description here