简体   繁体   中英

We cant access non static instance from a static method, but can initiate a class. how?

We cant access non static instance from a static method. But main() method is static and runs first. During initialization of any other class in main method, it will call the constructor. Is that constructor static ? what is the basic flow for JVM ?

The main method is called by the JVM to run the method which is outside the scope of project.

When the JVM calls the main method, there is no object existing for the class being called. So it has to have static method to allow this from class.

During initialization of any other class in main method, it will call the constructor.

If you mean instantiation , then Yes it will. Creating an instance of a class calls the constructor, whether the new call is made in main or anywhere else.

If you really do mean class initialization (which typically happens implicitly ), then No it won't. The initialization of a class does no involve the classes constructors.

For example

    public class Example {
        private static int foo = OtherClass.someMethod();
        static {
            // do something
        }
        public Example() {
            // do something
        }
    }

Class initialization executes the initializer for foo and the static initializer block, but is doesn't execute the constructor. Creating an instance of Example calls the Example() constructor.

Is that constructor static ?

Constructors are always static ... in the sense that new doesn't require an existing instance.

Yes, we can't access non-static variables from static block because, non-static variable are instance variables & can only be accessed by creating an object of class with new operator or using reflection like Class.newInstance() . Whereas, static variables are class level & it's value is constant for every single object. It means no need to create an object of a class to access those variables. You can access static variable by using class name (different class) or directly (within same class) like :-

public class HelloWorld {
    private static String message = "Hello";
    public static void main(String[] args) {
        System.out.println(message);
        System.out.println(HelloWorld1.sayHello);
    }
}

public class HelloWorld1 {
    public static String sayHello = "Hello1";
}

main() method :

public static void main(String[] args) {}

In Java, main() method is static & it's the entry point of JVM. Since, main() method doesn't belong to any class in Java . When we define main() method in any user-defined class, then it will belong to that class. And since it's static & within the same class, no need to access it using class name. The main method is directly available to JVM.

How JVM works :

When there is need to execute to any Java class ClassLoader comes into picture. The Java Classloader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine.

Java代码执行过程

Image taken from : Understanding JVM Internals

When JVM starts to execute a Java file, it'll

  1. First compile .java file & convert it into .class file which contains bytecode ie, machine language or assembly language. Each time the same bytecodes are processed, JVM works with JIT (Just-In-Time) compiler to convert byte code into native code .

  2. Loads the .java file & necessary packages using System Class Loader & BoostrapperClassLoader resp.

  3. After loading, JVM will look into .class file & store all information like variables, packages, methods, etc & save them into a memory & initialize all the field variables .

  4. The JVM then starts interpreting bytecode & displays the result of that in human readable form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM