I’m learning now Java from scratch and when I started to learn about instantiating objects, I don’t understand – in which cases do I need to instantiate objects?
For example I’m studying from TutsPlus course about it and there is example about “Rectangle” class. Instructor says that it needs to be instantiated.
So I started to doubt about – when do I need to instantiate those objects when writing Java code?
8
If you are coming from a background in dynamic programming languages, or new to programming generally, the new
keyword can be a bit confusing, especially in languages that don’t seem to always require it’s use. In Java it is not uncommon to see code like this:
User user = ApplicationEnvironment.getCurrentUser();
String username = user.getName();
int maxLength = 255;
String message = new String("Current logged in user is: " + username);
Hm, 4 variables and only one needed to be used with new
? What’s going on here?
The key is understanding the creation of an object vs the passing of a reference to an already existing object.
When we call new String("I'm a new string!")
we are asking the computer to create a new String object in memory and hand us back a reference to it, and we usually will be assigning that reference to a variable (as we did with the last line in my example.
So why don’t any of the other examples require the new key word?
Well, with the int line it’s a little weird – it’s a “primitive”, and a primitive isn’t an “object” and doesn’t need the new
keyword ever. It’s confusing at first, but if you just remember that new
only applies to objects and not primitives, you’ll be ok. You can look into exactly why that is, but you’ll be ok to press on if you don’t understand why – just that it’s how it works in Java for now.
Now, with the user and username variable the story is completely different.
What’s happening is that an object has been created with the new keyword somewhere else, and we are just getting a reference to an object that some other unseen code has created for us!
So, the new keyword is when we want to tell the computer “I need an entirely new object created and saved into memory, please.” When we are just assigning “references” to existing objects to different variables we don’t need new!
Now, this should cause you to wonder “what exactly is a reference?” Which is precisely what a good course should explain to you later on. You’ll get there if you stick with it!
For now, the rule to learn is:
- If you need a NEW object, you’re going to need the new keyword.
- If you need a reference to an existing object – one that already
exists or that some other code will create for you – then you don’t.
2
In object oriented programming, a class is a construct that defines a collection of properties and methods. It can be viewed as a template. For example, in your case Rectangle . Then we have the instance of the class which share the common structure that the class defines. This common structure consists of the properties (may be the length, width and color) and methods (methods like; getArea(), rotate() in the above example) of the class. However, the properties of different objects are different.
Why do you need them :
Without creating objects for a class, there is no use of the class. For example , You just create a Rectangle class, there is no use of that class in your program. But when you start modelling your solution in terms of classes, You will be modelling in terms of Objects like rectangle1, rectangle2 and so on , it would be the real model of the domain that you are working on.