70 likes | 217 Views
Set & Get Method s. Prepared by: Afra`a S. Al- Shammari. Set & Get Methods. Set and Get methods are used to retrieve and modify the attributes of a class. And as your variables are private, they won't be accessible without these methods. Set Method (Setter).
E N D
Set & Get Methods Prepared by: Afra`a S. Al-Shammari
Set & Get Methods • Set and Get methods are used to retrieve and modify the attributes of a class. And as your variables are private, they won't be accessible without these methods.
Set Method(Setter) • It is used to assign the values for the variables. - Features: • Public. • Void (do not have a return type). • Hold attributes.
Set Method(Setter) • Syntax: Public VoidsetVariable( var type var name){ this.var=var} - Example: • class A{ • int x; • Public void setX(int x){this.x = x;} • }
Get Method(Accessor) • It is used to retrieve the values of the variables. - Features: • Public. • Have a return type. • Do not hold any attributes.
Get Method(Accessor) • Syntax: Public VarReturnTypegetVariable( ){ return(var); } - Example: • class A{ • int x; • Public intgetX(){return x;} • }