Battleships were certainly getting on. But I wouldn't say they were obsolete in 1939; even in 1945 they certainly had their uses as gun platforms and in fighting off other BBs.
Moreover, one has to look at it from the perspective of interwar naval planners. Naval airpower in 1939 was a very sketchy prospect still; aircraft weren't very advanced, and particularly for powers focused their naval development outside the Pacific (i.e., everyone but the USA, Japan, and to a lesser extent Britain) there's little to suggest land based air can't to an extent fill the hole better (more advanced aircraft, cheaper basing requirements). The superiority of the carrier over the battleship wasn't an outright truth at that point, and it only looks that way with our hindsight. Even that in itself may be a product of the circumstances of the war; the two major potential battleship clashes were the Pacific (USA vs Japan) and the Mediterranean (UK vs Italy), and in both cases BBs were relegated to a background role as much by circumstance as anything else (i.e., the USA having to rely on carriers due to BB losses). There's also the risk to consider; if you don't build BBs and everyone else does, then how can you be sure you have a counter to other BBs, how can you match their ability to provide shore bombardment, and what do you do if through bad luck or otherwise it proves possible for BBs to close the distance before they can be sunk, and they go on to trash a carrier fleet at close range?
One can also point to times when BBs sank carriers, or came close to doing so; Scharnhorst sank Glorious early in the war, whilst even later on at Leyte the Japanese almost got a chance to cause havoc.