Before WW1 Italy was part of an alliance with Germany and Austria-Hungary, yet it didn't join them when the war started and it even joined the Allied side later during the war. Why did Italy do this? And if there were good reasons to join the Allies why did it ally itself with Germany and Austria in the first place?
Italy's main issue was its enmity with Austria-Hungary, Germany's main ally. That made Italy the "odd man out" in the so-called Triple Alliance with the other two.
Italy had joined (reluctantly) with Germany out of a fear of France. This occurred when France and Britain concluded an alliance that made Britain responsible for the mutual defense of the English Channel, and freed the French fleet to concentrate in the Mediterranean, possibly against Italy.
When World War I broke out, Italy found that it had nothing to fear from France (or England or Russia for that matter). On the other hand, it would have a lot to fear from a victorious Austria Hungary, from which she had taken Lombardy and Venice in the 19th century (the former when allied with France). So when Britain and France offered Italy Tyrol and Trieste from Austria, Italy jumped at the bait and switched sides.
It is easy to explain why Italy didn't join the war: they had little to gain from it, maybe they also didn't feel prepared. Alliances are always theory and a country can refuse to be dragged into a conflict with powers that are much stronger than it.
The question why Italy later still decided to join is more difficult. This website gives the following answer: