Unit1 - This Garbage
Unit1 - This Garbage
1. An instance variable in Java is a variable that is defined within a class but outside of any method,
constructor, or block. Each object of the class has its own copy of the instance variable.
Instance variables are also known as member variables or fields.
They are created when an object is instantiated and destroyed when the object is garbage
collected.
2. A local variable in Java is a temporary variable that is only available within a specific function or
block of code. Local variables are created when a function or block is entered and destroyed when it
exits.
Local variables are declared in a function body, block, or loop statement
3. A static variable in Java is a variable that is associated with a class, rather than with a specific
instance of a class. This means that all instances of a class share a single copy of the variable.
How to declare a static variable
Use the keyword static to declare a variable
Declare the variable outside of any method, constructor, or block
Static variables are created when the program starts and destroyed when the program ends
The value of a static variable is preserved across function calls and instances.
this keyword
The this keyword in Java is a reference variable that refers to the current object. It is used in
instance methods and constructors to access the current object's members.
Example: 1
// Java code for using 'this' keyword to
// refer current class instance variables
class Test {
int a;
int b;
// Parameterized constructor
Test(int a, int b){
this.a = a;
this.b = b;}
void display() {
// Displaying value of variables a and b
System.out.println("a = " + a + " b = " + b);}
Output
a = 10 b = 20
Garbage Collection
Garbage Collection is a key feature of the Java programming language that automatically manages
memory allocation and deallocation for objects that are created in an eden space.
Garbage Collection in Java allows developers to focus on writing code without worrying about
memory management, making Java a popular choice for building complex and large-scale
applications.
Garbage collection in Java is an automatic memory management process that helps Java programs
run efficiently.
Java programs compile to bytecode that can be run on a Java Virtual Machine (JVM). When Java
programs run on the JVM, objects in the heap, which is a portion of memory dedicated to the
program.
Eventually, some objects will no longer be needed. The garbage collector finds these unused
objects and deletes them to free up memory.
In C/C++, programmers have to manually create and delete objects, which can sometimes lead to
memory leaks and errors when there isn’t enough memory available. This happens when unused
objects are not deleted properly.
In Java, this process is automated. The garbage collector automatically finds and removes objects
that are no longer needed, freeing up memory in the heap. It runs in the background as a daemon
thread, helping to manage memory efficiently without requiring the programmer’s constant
attention.