As representative head of the human race Adam was given this command when he was alone, before Eve was created. As his Creator, the Lord had the perfect right to command Adam what he may or may not do.
Would Adam gain the knowledge of good and evil by eating of the tree or by not eating? It was God who gave the tree this name. Satan tempted Eve to believe that it was this knowledge that she was being deprived of by God and that she would gain by eating. But this was a great lie. Eve already had some knowledge of good, for Adam and Eve were created upright moral beings. How would they gain knowledge of good by eating of the tree? It is the lie of Satan that we cannot know evil except by experiencing it, but this is a trap. In fact those who seek to know evil by experience are blinded by it so that they cannot understand it as they should. Having tasted evil, it remains before them as a mystery drawing them ever more deeply into its power. The true knowledge of evil comes through diligently avoiding it and seeking righteousness. It is as we practice good that we have knowledge of good and evil, for we become those who ‘by reason of use have their senses exercised to discern both good and evil’ (Hebrews 5:14). Who can understand evil more clearly than God who is infinitely removed from it? Those who live immersed in sin are those who have been most dulled by it. Adam was commanded to make a choice between good and evil, between obedience and disobedience, which would bring the distinction between the two into sharp focus. It was by not eating that he could gain the knowledge of good and evil.