program to call private member in different class using setter getter concept
If you declare private member in a class and want to call different class then you can you use setter getter concept.
Below the program mention using setter getter method.
Create multi.java class
package add_returnvalue;
public class multi {
private int a;
private int b;
public void setA(int a){
this.a= a;
}
public int getA(){
return a;
}
public void setB(int b){
this.b= b;
}
public int getB(){
return b;
}
}
create main class multi_mainclass.java
in this class calling the private member from the other class
package add_returnvalue;
public class multi_mainclass {
public static void main(String[] args) {
multi ml = new multi();
ml.setA(20);
ml.setB(10);
System.out.println(ml.getA()*ml.getB());
}
}
Output:
0 comments: