In relation to Java A What is constructor the purpose of def

(In relation to Java)

A. What is constructor? the purpose of default constructor?

B. How do you get a copy of the object but not the reference of the object?

C. What are static variables and instance variables?

D. Compare checked exception and unchecked exception

Solution

A.

The constructor can be identified as a special kind of member function that has the name same as the class name.

The purpose of using the constructor is to initialize the member variables of the class at the time of object creation.

It is automatically called when the object is created while execution of a program. It does not have any return type because its purpose is to initialize the member variables.

B.

Using the copy constructor.

The copy constructor takes the object of the same class as an argument. This object is previously created and initialized. Then copy constructor takes it and initializes the member variables as per the object passed in the argument. So, basically it gives a copy of an object, but as a new object. So, the reference is not the same.

C.

static variables are class variable whereas instance variables are object variables. That means there is only 1 copy retained for the static variable throughout the class (for every objects) whereas for instance variable, each object creates its own individual variable. For example, static variable can be used to count number of objects created for the particular class.

D.

Checked exceptions are the exceptions that are checked during compile-time. If the programmer has not provided the mechanism of how to deal with this kind of exceptions, then it will give compilation error. Unchecked exceptions are not checked at compile time. Generally they occur at run time when user input some data which is not valid as per programming code written.

NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException are some of the unchecked exception whereas ClassNotFoundException, IOException, DataAccessException are some of the checked exceptions.

I tried to write these in simple words as I could. If you need more on any of these, do comment. I will add. Thank you so much. :)

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site