0

I am new to java and I am trying to understand the sequence in which JVM works.I have following queries.

1)Can a class be loaded at run time.

2)Can static variables be allocated memory during runtime.

3)Why static variables cannot be defined inside a function in java?

May be if you can explain me with the help of an example given below:

public class Test{
             public static void main(String[] args)throws IOException {
             static int d;
   }
}

In this example static is written inside a method which will give an error.It would be helpful if you can explain this with above context.

1 Answer 1

1

simply answers

  1. Yes
  2. Yes/No

details

  1. You can load any java class in runtime, usually this is done via Classloader
  2. All static variables are actually allocated and initialized by JVM (not you) during runtime, usually just before you use them, check out this answer: Order of initialization of static variable in Java

also you can re-allocate not final static variables in your code with new values/memory, like:

static String a = "a"; // default value to be used by JVM during init
public static void main(String[] args)
{
    System.out.println(a); // will print a, as JVM already initialized it with "a"
    a = "b"; // we've changed value
    System.out.println(a); // now will print b
}

but you cannot define static variable inside function, it is possible in c/c++, but not in java

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.