After reading this for decades, it suddenly occurred to me that Psalm 51 may have been a confession made publicly. The Bible doesn't say that it was, but apparently most of the Psalms were designed to be sung in public worship by the Israelites, and the heading of the Psalm, in the ESV, begins with "To the choirmaster," indicating that perhaps David expected that the Psalm would be performed as part of the worship of Israel. It is unlikely, with the palace servants (and perhaps some jealous wives), the prophet Nathan, and Joab, head of David's army, knowing the facts of the case, that David's actions could have been a secret. More likely they were public gossip. I understand that we can't know this for sure. The story may have been known only to David, Bathsheba, Joab and Nathan. David may have written Psalm 51 as his private confession to God, and some later writer saw to it that the public heard and read it, after David's death. But it's interesting to think about.
The application, of course, is that whether or not our sins are public knowledge, we need to confess them. No matter who else might have been hurt, God is the first person we should seek forgiveness from.
Thanks for reading.