This discussion is archived
4 Replies Latest reply: Dec 16, 2012 10:00 PM by EJP RSS

JVM class loading mechanisum

nitul.kukadia Newbie
Currently Being Moderated
Hi All,
As we all know in Java Application we have static members in at class level which are initialized while class is loaded first time in JVM.

So, let say i have a class TestStatic :
public class TestStatic {
     public static int Var = 0;
}

Now, i creates two application :

1) public class App1 {
     public static void main(String[] args) {
          System.out.println(TestStatic.Var);
          TestStatic.Var = 100;
          while(true){}
     }
}

2) public class App2 {
     public static void main(String[] args) {
          System.out.println(TestStatic.Var);
     }
}

So, I first run App1 : it will set TestStatic.Var to 100.
Then I run my App2 : it display TestStatic.Var to 1.

So, what is happening ? Or the Class loading is performed at Application level?
Can anybody explain the JVM mechanism for the same.

Edited by: nitul.kukadia on Dec 14, 2012 7:32 PM
  • 1. Re: JVM class loading mechanisum
    gimbal2 Guru
    Currently Being Moderated
    nitul.kukadia wrote:
    Hi All,
    As we all know in Java Application we have static members in at class level which are initialized while class is loaded first time in JVM.
    I'd say it is initialized when the class is loaded by a classloader, not specifically the JVM.
  • 2. Re: JVM class loading mechanisum
    Kayaman Guru
    Currently Being Moderated
    nitul.kukadia wrote:
    So, what is happening ?
    You have 2 JVMs running. The values won't magically jump from one process to another.
  • 3. Re: JVM class loading mechanisum
    nitul.kukadia Newbie
    Currently Being Moderated
    Ok. It means when I run any Application a separate JVM will be Running.

    So , What is the cost for those JVM running?
    How much memory they consume, and CPU usage.

    I have no Idea about how Application runs over JVM, class loading.
    So,
    Why JVM is loaded per Application?

    Why JVM does not perform class loading mechanism per Application?

    Does JVM is capable to run multiple Applicati?

    IF single JVM there will be running multiple Application and JVM will separate the memory allocated to each
    Application?
  • 4. Re: JVM class loading mechanisum
    EJP Guru
    Currently Being Moderated
    Ok. It means when I run any Application a separate JVM will be Running.
    No. It means every time you execute the 'java' command you start a new JVM.
    So , What is the cost for those JVM running?
    On what platform? With what arguments?
    How much memory they consume, and CPU usage.
    Is there a reason why your existing operating system utilities aren't telling you that? It is highly system-dependent and indeed invocation-dependent.
    Why JVM is loaded per Application?
    See above.
    Why JVM does not perform class loading mechanism per Application?
    Because class loaders do. You've already been told that.
    Does JVM is capable to run multiple Applicati?
    Yes, if you code one of them to start the other, or provide a starter application that starts them both.
    IF single JVM there will be running multiple Application and JVM will separate the memory allocated to each
    Application?
    Only if they are loaded by different class loaders. Which is one reason for having them.

    I suggest you have some reading to do. This is not the place to be learning this stuff.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points