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?
-
**[Unclear what help you need](http://meta.programmers.stackexchange.com/questions/6559/why-is-research-important "see: 'Why is research important?'").** Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it’s hard to tell what problem you are trying to solve or what aspect of your approach needs to be corrected or explained. See the [ask] page for help clarifying this question. – gnat Aug 22 '14 at 19:34
-
I need to know when I need to instantiate objects in Java? Because, clearly enough, I wouldn't do that if I didn't know when to use it. I see no logic there why to instantiate objects. I can't make it more clear enough. – davisdev Aug 22 '14 at 19:36
-
http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html might be able to aid your understanding. If you could be more clear about what you understand (like how a `int` doesn't need `new` but `String` does) vs what doesn't make sense, perhaps people could be more helpful. – BrianH Aug 22 '14 at 19:38
-
1Do you understand what classes and objects are? This is a very basic topic which makes me believe you are new to programming. It sounds like you might need help understanding the fundamentals, but the question is worded in a way that makes it difficult to answer. – Aug 22 '14 at 19:44
-
Before this stuff I was just a front-end programmer, but it seemed to me that - exactly that is not for me, so - now I'm seperately working with PHP and Java. I'm new to object-oriented programming so it's difficult for me to understand these things. – davisdev Aug 22 '14 at 19:46
-
Okay. It seems that I should ask it few days later because I think that when creating objects, keyword "new" wasn't the only way to create objects. I confused myself within it. – davisdev Aug 22 '14 at 19:59
-
I think I get what you are asking now - but if any of the answers misunderstand your question, let them (us) know and we'll try to help you refine your question or clarify our answer. – BrianH Aug 22 '14 at 20:06
-
Thanks, BrianDHall! :) It seems more clear to me right now. But these essentials are important to me now, because - last time I left of I didn't know essentials pretty good. So - it should be better if I start to understand more clearly it on the next day. Mind would be clearer. – davisdev Aug 22 '14 at 20:12
2 Answers
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.

- 6,092
- 1
- 21
- 23
-
When reffering to paragraph ("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!") you say that the object has been created somewhere else, okay, that's what I understand now - thanks! Somewhere else on stack' was topic about - when to use new keyword? Well, someone said that it goes from experience or he can look up to java docs to see if that concrete object requires "new" keyword (or info about - is it created already?). – davisdev Aug 23 '14 at 11:42
-
For example, I can't find there anything about something like: "new Rectangle()"; or something like that. I took a look in constructors table, nothing about "new" keyword. So it seems (in examples there) that it can be created, just by typing "Rectangle()". It confuses me. – davisdev Aug 23 '14 at 11:44
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.

- 2,627
- 15
- 23