0

Note: I'm a Beginner/Intermediate lvl java programmer so please be nice :S

I came across this lesson about how to compare 2 objects and I noticed this way of creating an object/instance which is completely new to me.

 MyValueObject myValueObject = (MyValueObject) obj; 

Q: How to put my parameters to pass it to the constructor for this object using this way?

What I'm used to and most of beginners too is:

 MyClass Object = new MyClass( my parameters ) ;

Thank you.

3 Answers 3

6

That's not instantiation, it's called casting. It does not create a new object, but rather it uses obj as a MyValueObject object. It basically says that obj is really a MyValueObject and it can be used as one.

Sign up to request clarification or add additional context in comments.

Comments

2
MyValueObject myValueObject = (MyValueObject) obj; 

doesn't create, but casts obj to a MyValueObject

Comments

0

In that first section, you are just assigning a previously instantiated object to the myValueObject variable. The object is already constructed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.