1.Explain the following : (a) Get methods (b) Set methods (c) A Constructor (d) Final variable
1.Explain the following :
(a) Get methods
(b) Set methods
(c) A Constructor
(d) Final variable
[Also add example codes to show the syntax]
Question - 1
(a) Get method - It is used to return the value of a variable.
(b) Set method - It is used to set value of a variable.
(c) Constructor - It is called when instance of class (object) is created.
It is used to initialize object.
Example -
public class Main
{
public String Variable;
// get
public String getVariable()
{
return Variable;
}
// set
public void setVariable(String newVariable)
{
this.Variable=newVariable;
}
public static void main(String[] args) {
// object of class Main and Constructor
Main obj=new Main();
obj.Variable="Hi";
System.out.println(obj.Variable);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 3 images