Variables
Instance Variables (Fields)
- Memory is allocated for instance variables when an object is created. Each instance of the class has its own copy of instance variables.
public class MyClass {
private int instanceVariable;
}
Final Variables
- Memory is allocated - Constants are usually declared with the `final` keyword, and their values are known at compile-time. They are treated similarly to static variables but are inlined by the compiler.
public class MyClass {
private final int CONSTANT_VALUE = 42;
}
Static Variables
- Memory is allocated for static variables when the class is loaded into memory. They are shared among all instances of the class and belong to the class rather than a specific instance.
public class MyClass {
private static int staticVariable;
}
Local Variables
- Local variables are created on the stack when a method is invoked and destroyed when the method completes. They are not part of the object's memory allocation.
public void myMethod() {
int localVariable = 10;
}
}
Local Constants
- Local constants are treated similarly to local variables. They are created on the stack when a method is invoked.
public class MyClass {
public void myMethod() {
final int localConstant = 42;
}
}
Static Methods
- Memory is allocated for static methods when the class is loaded into memory. They are associated with the class rather than a specific instance.
public class MyClass {
public static void staticMethod() {
// ...
}
}
Local Static Variables
- Java does not allow the declaration of local static variables. Static variables are associated with classes, not methods or blocks.
public class MyClass {
// No local static variable allowed here
}
- Memory for instance, variables is allocated when an object is created,
- memory for static variables is allocated when the class is loaded, and local variables/constants are created on the stack during method invocation.
- Static methods are associated with the class and exist in memory when the class is loaded.
- There are no local static variables in Java.
Methods
Methods in Java are not allocate memory in the same way that variables are. Instead, the bytecode for methods is loaded into memory when the class is loaded. Let's break down the details:
Instance Methods:
- The bytecode for instance methods is loaded into memory when the class is loaded, but each instance of the class does not have its own copy of the method. Instead, all instances share the same set of methods.
- When an instance method is called on an object, a stack frame is created on the call stack for that method invocation, containing local variables, parameters, and the return address.
public class MyClass {
public void instanceMethod() {
// Method code
}
}
Static Methods:
- Similar to instance methods, the bytecode for static methods is loaded into memory when the class is loaded. Static methods are associated with the class rather than with instances.
- When a static method is called, it is invoked using the class name, and no instance of the class is required.
public class MyClass {
public static void staticMethod() {
// Method code
}
}
Local Variables in Methods
- Local variables within methods are created on the stack when the method is invoked and destroyed when the method completes.
- Each method invocation gets its own stack frame with space for local variables, parameters, and other method-related information.
public class MyClass {
public void myMethod() {
int localVar = 42;
}
}
- Methods themselves (the bytecode) are loaded into memory when the class is loaded.
- When methods are invoked, stack frames are created on the call stack to handle the local variables, parameters, and other information specific to that method invocation.
- The methods are shared among all instances for instance methods and belong to the class for static methods.
No comments:
Post a Comment