Instantiating objects in Java [closed]

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:

  1. If you need a NEW object, you’re going to need the new keyword.
  2. 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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật