+ 23
How many ways we can create an object in Java?
How many ways we can create an object in Java?
14 Respostas
+ 12
As you know, a class provides the blueprint for objects; you create an object from a class.
• Declaration: The variable declaration that associate a variable name with an object type.
• Instantiation: The 'new' keyword is a Java operator that creates the object.
• Initialization: The 'new' operator is followed by a call to a constructor, which initializes the new object.
+ 11
M_N 👍You are welcome! 😊
If you do not want to research (To make learning more fun and interesting . . .) to find the answer, then you will find it difficult to learn!
As long as someone explains to you, while you do not find a way to overcome your problem, you will not understand!
The dirty little secret of programming is . . .
You will never know everything you need to know to solve all your problems. Going into the journey, you probably envisioned yourself eventually learning everything you need to learn, and then being squared away. ➝ "This moment will never happen . . ."
Have a nice coding friend! 😉
+ 10
1) ObjectType obj = New ObjectType();
2) ObjectType obj = SomeClass.createNewObject();
Where ObjectType and SomeClass are 2 classes and createNewObject is a static method of SomeClass that is a defined method of the class.
+ 10
5 Different Ways to Create Objects in Java:- https://dzone.com/articles/5-different-ways-to-create-objects-in-java-with-ex
+ 10
M_N
I mean in general, my words did not refer directly to you, do not misunderstand me.
I'm sorry if I insulted you, I did not want to.
Every help is welcome, we all want to help as much as we can, each of us brings a different opinion and that is important, the author will take what is right for him.
Since you know a lot about this topic because you are asking the right questions, I wanted to get it from the author, his interest, what the discussion brings to us is important.
I think, on this way we can learn so much, so we show a desire for progress.
but, that's just my opinion!
Have a nice day! 👍😉
+ 8
For example:
Each of the following statements taken from the CreateSLObject program creates an object and assigns it to a variable:
User originOne = new User(name, age);
Sololearner userOne =
new Sololearner(originOne, name, age);
Sololearner userTwo =
new Sololearner(name, age);
The first line creates an object of the User class, and the second and third lines each create an object of the Sololearner class.
Each of these statements has three parts [ already discussed: 😊]
+ 6
M_N When you want to mention someone specific then write:- @UserName
When it comes to examples, you have a specified link on wich you can find besides excellent explanations, appropriate examples.👌
I have listed the ways in which objects can be created, if you want to learn, then you will write your example and thus best understand.😉
I can show you, I can direct you where to search for the answer, but it remains for you to do it!👍
If you don't produce, you won't thrive ⟹ no matter how skilled or talented you are! 🍻
+ 5
Danijel Ivanović, tnx for advice. I didn't know about '@'. I use sololearn with android, so I prefer to switch between it and browser in case if I need some new information. You can see that part of users asking simple and not questions even if they can find answers on a first page of google. And they ask questions here not because they don't know how google it, but because they get not the simple answers. So there is no sense to post links... they don't use them:)
Thats why I tried to explain something realy hard for understanding with simple examples. I hope my examples are right.
+ 5
Danijel Ivanović, I am not the topicaster, so the question wasn't mine and just tried to give simple answer. You told about Instantiation 'keyword new create the object'. But you didn't told that author should use it with Constructor. For example:
new Date()
- we should understand it like "okay, now I use keyword 'new' to give part of memory for Date constructor". As we see 'new' don't create an Object by itself.
+ 4
Creation of object is:
new ObjectType();
Creation of the link:
ObjectType object_name;
Creation of the link to the new object:
ObjectType object_name = new ObjectType();
Don't forget that object_name is JUST a link to the object.
+ 4
I 'like' when people use instantiation/initialization/declaration without examples and simple description...
Declaration:
ClassName x; (now we have name of our new Object but we don't have an Object).
Initialization:
... = new ClassName(); (keyword 'new' put value into the memory using Constructor);
Instatiation:
... ... = new ...(); (keyword 'new' just gave some piece of memory for Object);
----
String x = new String("test");
Here we used declaration/instantiation/initialization.
----
StringBuffer str_buf = new StringBuffer(new String(Arrays.toString(char_array)));
Here we used instantiation and initialization with 'new String()' for connection between StringBuffer and array.
---
So you can't use keyword 'new' without Constructor. Initialization connected with instantiation.
+ 3
It's almost like in C#.
+ 2
You can also try a class constructor for easy data insertion
0
باود