How is it possible to have the main method inside the class where its containing the definition of the instance being called inside the main?

Viewed 718

Typically, I write the class definition and then make a separate class to write the definition of the main method to make my class instance and call its methods. However, I have seen that it is possible to have the main method inside the class itself that the main method is calling an instance of. How is that possible? I thought we could not create an instance of a class within the class itself, that just does not make sense.

Here is my example below: How is it possible to have a Test instance within the class definition itself?

 import java.util.*;

public class Test{

    public int tester()
    {
        return 5;
    }

    public static void main(String args[]) {
        Test x = new Test();
        x.tester();
    }
}
5 Answers

When you write

Test x = new Test();

It just a creates an instruction of how to create the object, and the compiler will just compile it not execute the line.

It is true the way you think that in Java until the Class definition is complete how it is possible to create an instance for the same, but It is just a compilation. It is not creating any object. The object will be created in runtime. During runtime, it will have the .class version of the Java File and when the static main method gets called by the JVM which doesn't need any instance, the main gets executed and will create the instance of the Class Test in the runtime.

So this is absolutely fine to write the instance creation code inside the main method as you have given in your example. You can always do that.

Try to see your class as a bucket that is unchangeable. When you execute your program you put some sand in your bucket and create a castle(object). At this point this is your first castle so you're gonna execute also your main method (after executing the constructor), the main program will create just another castle that is different from the one before, without executing his main because the program already started.

main is a static method and it's completely valid to create an instance of its class.

Bear in mind that

In Java, a static method is a method that belongs to a class rather than an instance of a class. The method is accessible to every instance of a class, but methods defined in an instance are only able to be accessed by that member of a class

you can do like this.

public class Test{
   static Test x= new Test();
    public int tester()
    {
        return 5;
    }

    public static void main(String args[]) {
        x.tester();
    }
}

From docs

The Java Virtual Machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java Virtual Machine then links the initial class, initializes it, and invokes the public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java Virtual Machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.

So, java would simply load the class you execute with java Test and executes the static method main which should be public and return type be void. Test object is not created till now but now it will be created as you have mentioned Test x = new Test(); in main method.

Related