[SPARK-3897]format example code and explicit val instead of var

standard usage is two line.
code separate with annotation as fa as possible, as scala can omit semicolon, not likely java