Difference between Declaration, Instantiation and Initialization of the objects in Java

Most of the time Java beginners get confused between the terminology "object","reference","instance". Also, there's a  misunderstanding between Declaration of object, instantiation of the object and initialization of objects. Find out the difference between three of them.

Before we continue remember one most important line "A reference refers (points) to an instance of an object."

1. Declaration of objects:

Declaration of objects in Java is similar as we declare variables of built in data types that is,
data_type   variable_name;

For example,
int  number;

Same way we can declare an object variable that refers to type “Class”. It takes the following syntax,
Class_name   object_variable;

For example,
Person  p;

will declare object variable “p” that refers to type “Person” in the memory heap.(Here we can consider our class to be a user defined data-type.)

If you declare “p” like this, its value will be undetermined until an object is actually created and assigned to it.

Simply declaring a reference variable does not create an object. For that, you need to use the new operator, as described in the next section.

You must assign an object to “p” before you use it in your code. Otherwise, you will get a compiler error.


2. Instantiation of Objects:

Once we have declared the object variable we need to instantiate it with an object of the corresponding data-type i.e. class.

The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.

In short, Instantiating the object means nothing but just allocating the heap memory of the declared object variable of class type.

For above example we can instantiate the object variable “p” like this,
p = new Person();

We can also declare and instantiate the object by following,
Person p = new Person();


3. Initialization of the objects:

The new operator is followed by a call to a constructor, which actually initializes the new object.

Thus, in above statement “ Person(); ” will initialize the actual object and will recognize the appropriate constructor.

Initialization concept can be more precisely understood when we have multiple constructor for the same class. For example,

public class Person {

int age = 20 ;

String name = "Mr. Lazy";

Person(){
System.out.println("Default constructor called.");
}


Person(int argument1){
System.out.println("Constructor with one argument called.");
}


Person(int argument1,int argument2){
System.out.println("Constructor with two arguments called.");
}


public static void main(String args[]){

Person p1 = new Person(); // this will invoke default constructor.

Person p2 = new Person(10); // this will invoke constructor with one argument

Person p3 = new Person(10,20);// this will invoke constructor with two arguments.

}

}

As we can see in this example the Java compiler differentiates the constructors based on the number and the type of the arguments. Thus, the object is initialized based on the arguments provided with it.

Happy coding! :)

"Safely remove your hardware" What's that and Why do we need it?

You might have seen in most of the OS it tells you to safely remove your hardware for instance a flash drive or usb drive. Have you ever wondered what does it mean by "safely removing your hardware"? Most of the time you can see that you pull out your pen drives without such procedures and no harm happened to it. Then why it is there? Here's a complete description to it. 

This is one of those questions that has a short answer and a long answer. The short answer is this: you should probably always eject a drive before removing it, even if the context menu doesn't have an eject option. Mac and Linux will always provide you a way to eject a drive, but like you said, Sometimes Windows doesn't have an obvious "Eject" button for certain drives. On Windows, click the "Safely Remove Hardware" icon in the system tray, choose your drive from the list, and then remove it once it notifies you of its safe removal.

Now, the long answer : In Windows, you can sometimes remove a flash drive without ejecting. Here's a bit more information on how computers deal with USB drives.

Why Computers Want You to Eject Drives:


Obviously, yanking out a drive while it's being written to could corrupt the data. However, even if the drive isn't activelybeing written to, you could still corrupt the data. By default, most operating systems use what's called write caching to get better performance out of your computer. When you write a file to another drive—like a flash drive—the OS waits to actually perform those actions until it has a number of requests to fulfill, and then it fulfills them all at once (this is more common when writing small files). When you hit that eject button, it tells your OS to flush the cache—that is, make sure all pending actions have been performed—so you can safely unplug the drive without any data corruption.



Why Windows Doesn't Bug You to Do It:


Do I Really Need to Eject USB Drives Before Removing Them?Mac and Linux use write caching on pretty much all drives, and will let you eject any drive through your file manager. Windows, however, is a bit more mysterious on this front. It actually disables this write cache feature for drives it sees as "removable", because it knows people are likely to yank them out without ejecting (though you can still eject them by right-clicking on them and pressing "Eject"). As such, disabling this feature on removable drives decreases the chance of data corruption. It keeps the cache enabled on non-removable drives, though—and sometimes it recognizes external USB drives as not removable, which is why your USB drive doesn't have an eject button. Paradoxically, it's also why you need to eject that drive: since Windows doesn't see it as removable, it's enabled the write cache for it, increasing the chance of data corruption.
You can edit the write cache settings for any drive from the Device Manager. Just expand the Disk Drives section, right click on the drive you want to edit, and hit Properties. Go to the Policies tab, and click the "Quick Removal" radio button to disable the cache (or click "Better Performance" to enable the write cache).

Tip : Always keep your disk in Quick Removal Mode.

Why You Should Probably Manually Eject All Your USB Drives Anyway:

Do I Really Need to Eject USB Drives Before Removing Them?
So, unlike OS X and Linux, Windows has a few precautions in place for preventing data loss. However, the write cache isn't the only thing that can cause data loss. Have you ever tried to eject a drive and gotten a "file is in use" error? Sometimes there's something going on in the background you don't know about, or sometimes a program is just being silly and has still locked a file on the drive even if it isn't using it. If you were to yank it out during one of these situations, you could still cause data loss. Ejecting it will warn you of the situation, and let you close the program in question (or use something like Unlocker to unlock the in-use file).


In the end, there's no reason not to eject your drives, and doing so will ensure your data is uber-safe. Windows users may be less likely to experience issues due to the way Windows handles removable drives, but they aren't 100% immune. Ejecting the drive is a great habit to get into, since without it, you wouldn't always know if it were safe to remove or not.

Got questions? Leave it in the comments.

Reference : LifeHacker