Constructors and Destructors in JAVA
Before we learn about the Constructor and destructor let us discuss how to create an object in JAVA programming language.
Creation of an object in JAVA:-
- We cannot create an object in java like C and C++ syntax like,
demo d;
- In C++, we can read this statement as d is an object of a demo class.
- But in java this statement is considered as d is a reference which can refer to an object of type demo class.
- Means in java, a memory for that d is only for that reference.
- To allocate actually memory for that object we have to use a ‘new’ functionality.
demo d = new demo();
- In this case, d is a reference which is currently referring to a memory allocated for an object of a demo class.
- In this case, a memory for the demo object is allocated on a heap memory but reference is created in a particular stack frame.
Now let us come to the Constructors and Destructors in JAVA.
Constructor and Destructor in JAVA:-
Constructor:-
- Java supports two types of constructors, i.e.
1) Default constructor
2) Parameterized constructor
- There no concept of copy constructor in JAVA.
e.g.
class demo
{
public int I;
public demo() // default constructor
{
i=10;
j=20;
}
public demo(int x,int y) // parameterized constructor
{
i=x;
j=y;
}
}
- In the above example, we can declare the constructor without any access specifier.
- Constructor is the first function which called after allocating the memory.
- In this example, there is no semicolon ‘;’ is used at the end of a class, because, in JAVA programming language we write everything in a class and in C++ we can write functions outside the class.
- So we use the semicolon ‘;’ in the C++ but not in JAVA.
- In java, an object must be created dynamically by using the keyword ‘new’.
demo d = new demo();
demo d1 = new demo(40,50);
- In above, d and d1 are considered as a reference.
- By using the keyword ‘new’ we allocate the memory for that object on a heap and ‘new’ keyword returns starting base address of that allocated memory and that starting base address is copied inside the references d and d1.
demo d2;
- Above syntax is considered as an object in C++ but java considers d2 as a reference that refer to a class demo.
- And object is a physical memory of previously allocated blueprint or a class
Copy Constructor:-
- In java, there is no concept called as copy constructor. But we can write a copy constructor in a java.
e.g.
class demo
{
int i,j,k;
demo(demo d) // copy constructor
{
i=d.i;
j=d.j;
k=d.k;
}
}
class hello
{
public static void main(String[] args)
{
demo d = new demo();
demo d1 = new demo(d);
// here the copy constructor is invoked.
}
}
Destructor:-
e.g. Destructor implementation in C++ is compared with destructor in JAVA is as follows,
class demo
{
public:
int i;
int *p;
demo()
{
p=(int *)malloc(40);
}
~demo()
{
free(p);
}
};
- From above example, such type of destructor is not required in java, because, java provides its own garbage collection technique.
- If programmable is unable to free a memory then garbage collector is used to free that memory.
- Like a C++ we can provide a destructor in java and that destructor is considered as a finalize method.
- Means for providing destructor we have to provide finalize method and inside that method we have to write a code for a destructor.
finalize()
{
// code for freeing resources which are allocated inside a constructor.
}
- In C++, we can predict the sequence of constructor and destructor’s calling sequence that sequence is depend on a type inheritance for the place at which object is created.
- But above point is not valid in java means in java we can predict constructor calling sequence but we cannot predict the destructor calling sequence.
- In the case of finalize() methods, we do not have to de allocate a memory because a memory freeing task is done by a garbage collection.
- So we just have to free only the resources from the finalize method.
Important points:-
- In java, we cannot explicitly free the memory because there is a automatic garbage collection technique.
- Garbage collector is a thread which runs periodically and collects un referenced object.
- There is no such method like free() and delete() in java but still we can derefer the particular object explicitly in our program and that derefer object is collected by garbage collector.
demo d = new demo();
d = null; // this is an explicitly dereferencing the object and now that object is collected by garbage collector.
Difference between Program, Process, Instance and Thread:-
1) Program:-
- Program is a set of a static instruction which are not executed yet and that instruction are in human readable format.
2) Process:–
- When we convert our program in executable format which is understandable by JVM and when we execute that code that code comes into the RAM and starts executing its instructions and it is called as Process.
3) Instance:-
- Instance is a term which is commonly used in MS Microsoft platform but in Linux platform there is no term called as an instance.
- When we run a notepad four times, the text, data and stack of a notepad program comes into the memory then OS considered it as a four different processes.
- But in this case, text section of all the notepad is same due to which memory for three text section is wasted.
- To avoid this drawback, OS copies text section only once but data and stack section of each and every notepad is different.
4) Thread:-
- When we run a program it becomes a process and each and every process has a single main thread.
- In simple term, thread is a function which runs simultaneously means single program contains more than one thread and if there is no thread then at least single thread is created.
- According to above points we consider that every process as a thread but every thread is not a process.
No comments: