Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Instantiating object in main method



This is a really, really dumb question but I'm trying to wrap my mind around why all my beginner java books use the main method to instantiate objects of a different class than the one it is being instantiated in, such as the one below. I've tested it and know it works but why don't I ever see it in the books? Is there a drawback to creating a new object of the same object that you are in?

Thanks, Von and sorry if this is a crazy question!!!!!!

public class TestClass
int id = 6;

{

    public static void main(String[] args)

     {
        TestClass firstTest = new TestClass();

        firstTest.id = 5;

        System.out.println(firstTest.id);
     }

}