-5

I'm a beginner in java and I have a question. Is calling a static method in class makes this class become instantiated? I mean , If I call a static method that exists in a class, will JVM create an instance of this class in memory ? If I have a class called X ,and a class called Y , the class Y contains a static method M that instantiates another class called Z that extends thread

public class X {
    public static void main(String[] args) {
        Y.M();
    }
}
public class Y {
    static void M() {
        new Z().start();
    }
}
class Z extends Thread {      
   public void run() {   
     ConnectToServer();
    }
}

Now how many instances of Y I'm gonna have in memory?

MM.Linuxer
  • 13
  • 4

1 Answers1

1

If I called a static method that exists in a class will JVM create an instance of this class in memory ?

No. Static methods are class-level methods. They are not called on an object (= an instance of a class) and it is not necessary for the JVM to create an instance of the class to call a static method.

The JVM will load and initialize the class (which means: it runs static initializers), but not create an instance of it.

More information: The Java Tutorials - Understanding Class Members

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • 1
    Would be better to say that "the JVM will *initialize* the class", because calling a static method will both load the class, *and* run static initializers. – Andreas Oct 06 '18 at 22:24
  • @Andreas thanks for your comment, added to my answer. – Jesper Oct 07 '18 at 13:21
  • great but what is the difference between Loading a class and making an instance of it – MM.Linuxer Oct 07 '18 at 14:08
  • and If I have a class like this public class Y { int x ; static void Main() { new Z().foo(); //1 //is equal Z.foo();//2 } static class Z { static void foo(){} } since all the members of this inner class is static so at 1 I'm creating an instance of the class and accessing the class through it which is discouraged at 2 I'm accessing it directly since the class itself and all it's member are static , booth sentences make the same things in JVM ? right – MM.Linuxer Oct 07 '18 at 14:13
  • @MM.Linuxer The JVM doesn't know about a class until it has *loaded* the compiled `.class` file definition into memory and processed it. Once the JVM knows the class, you can create *instances* of the class using the `new` operator. – Andreas Oct 07 '18 at 14:14
  • @MM.Linuxer *"both sentences make the same things in JVM?"* Both call the `foo()` method, but the first also creates an instance, which is immediately forgotten and hence immediately eligible for garbage collection. If you use a good IDE, it will warn you about that first one with a "non-static access to static member", so you can fix the issue. – Andreas Oct 07 '18 at 14:18
  • so the first one calls the garbage collector after the class is no longer pointed at by any reference but the second doesn't create an instance so does this mean that instance classes are faster than static classes , and uses less memory because instance classes gets removed when It's done , but static classes remains there as long as the application is working ? If I'm offered to use booth static class and instance class which would be better for performance ? – MM.Linuxer Oct 07 '18 at 14:55
  • @MM.Linuxer making an instance of a class means: creating an object from the class. See also: [The difference between Classes, Objects, and Instances](https://stackoverflow.com/questions/1215881/the-difference-between-classes-objects-and-instances) – Jesper Oct 07 '18 at 16:17