2D space game. How to have infinite starfield?

I am making a game, that involves the player controlling a ship, moving around and shooting. There are stars and planets in the background. The planets are stars are individually placed. )planets coordinates are placed manually, whilst 500 stars are randomised between + or - 1000x from the ships' starting point. where x, is the 'z distance' from the player's 2D plane. This implements parallax star movement.The ship is placed in the middle (always), pressing left and right rotates, press up accelerates.

This means I only have a starfield of size 2000*2000 (for the front stars, but the back stars go across a lot longer). I could make the starfield bigger, but it'd mean more stars (game runs slower), and in the end the starfield is still finite in size.

So, how to make an infinite star field? I don't want to have to limit the size of the map.

A lot about this is unoptomized for 2d (this is for a 3D game), and this is not my finest example of code (it was for 3DU and I was in a hurry), but it works. (it is Java, but I presume that you can see what I'm doing)

I couldn't get your code working, but I wrote new code based on yours. (because my stars are implemented differently to yours) I finally got them to "wrap", but there is one problem, the stars "flicks" to the position relative to the ship instead, not wrapping.

line (starsx[i]-ViewX/scaleis,starsy[i]+ViewY/scaleis,starsx[i]-ViewX/scaleis+1,starsy[i]+ViewY/scaleis+1);
line (starsx[i]-ViewX/scaleis,starsy[i]+ViewY/scaleis,starsx[i]-ViewX/scaleis+unit[player[thisplayer.thisplayeris].ship].vx/(scaleis*scaleis),starsy[i]+ViewY/scaleis-unit[player[thisplayer.thisplayeris].ship].vy/(scaleis*scaleis));