Thoughts may be ephemeral, but a tweet, apparently, is forever.

A Criminal Court judge in Manhattan ruled on Monday that Twitter must turn over to prosecutors messages sent by a Brooklyn writer during the Occupy Wall Street protests last fall. In doing so, the judge, Matthew A. Sciarrino Jr., indicated that although private speech was protected, the same did not apply to public comments on Twitter.

“The Constitution gives you the right to post, but as numerous people have learned, there are still consequences for your public posts,” Judge Sciarrino wrote. “What you give to the public belongs to the public. What you keep to yourself belongs only to you.”

The writer, Malcolm Harris, was one of about 700 protesters arrested in October while walking on the roadway of the Brooklyn Bridge. He was charged with disorderly conduct, a violation. In January, the Manhattan district attorney’s office subpoenaed all messages that he had posted to Twitter from two days before the Occupy Wall Street protests began in September through the end of 2011.

Mr. Harris’s messages, which are no longer publicly available, are of interest to prosecutors because they may contradict an argument they expect him to make at trial: that the police led or escorted protesters off the pedestrian paths and into the bridge’s roadway.

His lawyer, Martin R. Stolar, filed a motion to quash the subpoena, saying it had not been delivered properly, was overly broad and was issued for an improper purpose.

Judge Sciarrino dismissed the motion in April, writing that Mr. Harris lacked the standing to oppose the subpoena because under Twitter’s policies, he had granted the company the “worldwide, nonexclusive, royalty-free” right to distribute his messages, which were all publicly available.

Twitter itself then sought to quash the subpoena. But in the decision released on Monday, Judge Sciarrino ordered it to turn over Mr. Harris’s messages. While noting that laws regarding social media were evolving, he held that public speech, regardless of the forum, did not enjoy the same protections as private speech.

The case over a minor criminal charge raises significant issues in the still-new area of social media.

As is becoming more common, prosecutors requested Mr. Harris’s postings and member information directly from Twitter, which then notified Mr. Harris. The American Civil Liberties Union filed a friend-of-the-court brief in the case, arguing that prosecutors should be required to obtain a search warrant from a judge for each request and that the person whose posts were being sought should have an opportunity to oppose the demand in court.

For law enforcement, social-media postings that document a person’s immediate thoughts and precise location provide a potent tool.

“We are pleased that the court has ruled for a second time that the tweets at issue must be turned over,” said Daniel R. Alonso, the chief assistant district attorney in Manhattan. “We look forward to Twitter’s complying and to moving forward with the trial.”

Twitter had argued that demands for postings and account information placed it under an undue burden of either turning over the information or fighting each request on behalf of its users.

In a statement, the company said it was “disappointed” with the ruling and was considering its options.

“Twitter’s terms of service have long made it absolutely clear that its users own their content,” the statement said. “We continue to have a steadfast commitment to our users and their rights.”