Hope you are doing well.
Closure is all about scope when Scala constructs a closure, it determines which outer variables the closure will use and stores references to them in the closure object. This allows the closure to work properly even when it's called from a different scope than it was created in.
Please kindly check the attachment for for more details.
Let us know if you want anymore explanation in spark closure.
We will wait for your response.