How can a public method return the reference for the private instance to another class
Posted: Sun May 03, 2020 1:10 am
public class Test {
private String writer;
private Data dd; //private instance reference variable
public Test(int val) {
dd=new Data();
dd.instt=val;
}
public Data getAuthor() {
return dd; // returning the reference of it to another class. Which is suppose to not happen, but happens Why ?
}
}
class TestC{
public static void main(String[] args){
Test tob = new Test(12);
Data oo = tob.getAuthor(); //reference returned here
oo.instt=12; //Access provided for private instance
}
}
private String writer;
private Data dd; //private instance reference variable
public Test(int val) {
dd=new Data();
dd.instt=val;
}
public Data getAuthor() {
return dd; // returning the reference of it to another class. Which is suppose to not happen, but happens Why ?
}
}
class TestC{
public static void main(String[] args){
Test tob = new Test(12);
Data oo = tob.getAuthor(); //reference returned here
oo.instt=12; //Access provided for private instance
}
}