function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Developer BaseDeveloper Base 

What does this mean in java? (Java fundamentals question!)

Hey whatsup,

what does this mean?
 
Product2 pro;
            if ((pro = (Product2)Tools.findRecordByValue(pros, 'Id', rec.Product2Id)) != null) {
... some code here
}

Tools is a class I have a method called findRecordByValue, but WHAT IS THIS????
 
(Product2)Tools.findRecordByValue
Why is (Product2) before the Tools method???

Can someone give me a link to explanation about what this is? It seems like a java fundamental thing.

 
Best Answer chosen by Developer Base
Footprints on the MoonFootprints on the Moon
What I understood reading your snippet is that,
pro is of data type Product2 and Tools.findRecordByValue is returning some data type.
In order to assign the returned value to pro, we must ensure both are of same type.
Hence, with (Product2) Tools.findRecordByValue we are type-casting the returned value to Product2.

FYR - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_casting.htm

Hope this helps!

All Answers

Footprints on the MoonFootprints on the Moon
What I understood reading your snippet is that,
pro is of data type Product2 and Tools.findRecordByValue is returning some data type.
In order to assign the returned value to pro, we must ensure both are of same type.
Hence, with (Product2) Tools.findRecordByValue we are type-casting the returned value to Product2.

FYR - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_casting.htm

Hope this helps!
This was selected as the best answer
Developer BaseDeveloper Base
Ah nice. That's exactly what I was searching for. thanks