You can buy train tickets for anywhere in Indonesia online from this website: https://en.tiket.com. You can also buy a ticket at any IndoMaret (like 7/11) but train tickets may sell out on popular routes. Once you book on the website, this will then email you an e-ticket, which you take to the check-in counter at the station and they will give you your physical tickets.
IMPORTANT - ask here at the counter if you can have a carriage further to the back. The train horn sounds for approximately 75% of the trip, and if you don’t have earphones or aren’t good at zoning out abrupt noises, this can be really tiresome. The executive carriages are at the front, so if travelling executive, ask how many executive carriages there are. If there are 5, ask for a seat in carriage 5. If you’re in economy, ask how many economy carriages there are and get one closer to the back.
Economy class is fine and gives you pretty standard upright train seats - it’s nothing flash but pretty standard for any train anywhere. Executive class gives you seats with heaps of extra leg room, a little table that pulls out of your chair arm, a power socket to charge your phone, a blanket, and your seats recline - enough so that I fell asleep for three hours. And the train staff will come and wake you up at your stop. I usually am happy to travel for the cheapest option, but after a very long day I was glad for the extra comfort (we woke at 1:30am for the hike and boarded the train at 7pm after a full day of exploring and adventuring!).