Since you just started coding, you won’t give you a term like reflection, and that’s it .. here’s one of the easiest ways to get the public getter() method.
Consider this simple example.
class Something { private int a=10; public int getA() { return a; } }
Here is the first one that has a public method that returns the object that I created in this class for the Something class
class MyFirstClass { private Something st; public MyFirstClass() { this.st = new Something(); } public Something getSt() { return st; } }
Access to it from another class
class MySecondClass { public static void main(String...strings ){ MyFirstClass my =new MyFirstClass(); System.out.println(my.getSt().getA()); } }
Output: 10
If you have not checked
Enter this function in MyFirstClass
public void printHashcode(){ System.out.println(st); }
and then print the hash codes of both methods in MySecondClass
class MySecondClass {
public static void main(String...strings ){ MyFirstClass my =new MyFirstClass(); System.out.println(my.getSt()); my.printHashcode(); }
}
You will see that you are indeed using the object created in MyFirstClass in MySecondClass .
Because it will give you the same hashcode output.
Conclusion On my machine.
Something@2677622b Something@2677622b
Ankur anand
source share