Call function from calling object - java

This is for a personal project. Not assignment or work.
Say I have an object, objA that has a function callB().
When I run callB() it calls a function in object B. The function in objB can have calls to functions in objA.
Eg. objA calls callB().
Inside callB() there is a function like setObjAName() which sets a variable on objA.
How would I do this in Java? How do I reference objA from objB?

The simplest method is to simply pass a reference to A in with the method call, which will allow for B to access any of A's public methods.
public class ClassA {
public String someAVar;
public void callB(ClassA a){
//do stuff
ClassB b = new ClassB();
b.setObjA(this,"newValue");
}
}
public class ClassB{
public void setObjA(ClassA A, String newValue){
A.someAVar = newValue;
}
}
Alternatively you might want the variable to be settable without passing in a particular instance, in which case static methods and variables are your friend.
public class ClassA {
public static String someAVar;
public void callB(){
//do stuff
ClassB b = new ClassB();
b.setObjA("newValue");
}
}
public class ClassB{
public void setObjA(String newValue){
ClassA.someAVar = newValue;
}
}

Related

How to pass value by reference in this case?

I need a way to check if a boolean value of another object is true, and only in that case should do something.
So, I have 2 classes:
public class A{
private boolean test = false;
...
...
public boolean returnTest(){ return test; }
}
public class B{
private boolean abcd;
public B(A myA){
this.abcd = myA.returnTest();
}
public void test(){
while(!abcd){
// wait
}
// do something
}
}
The problem is that it remain in the while loop forever even if the test value of the object A become true. Why ?
In your case, pass instance to function test() is better, invoke test() with passing instance of A. Everytime an new instance of A is created or an existing instance has updated its attributes, pass it to test()
public void test(A a){
while(!a.returnTest()){
// wait
}
}
There is no real alternative to changing test() to do while (!myA.returnTest()). If you store a result in a variable it will stay permanently. (Properties of objects, or method calls, can reflect changes elsewhere, but variables themselves cannot.)
Booleans are immutable so you cannot make a copy of a reference to them.
Instead, try:
public class B{
private A a;
public B(A myA){
this.a= myA();
}
public void test(){
while(!myA.returnTest()){
// wait
}
// do something
}
}
In Java 8 you can use a method reference.
public class A{
private boolean test = false;
...
...
public boolean returnTest(){ return test; }
}
public class B{
private BooleanSupplier abcd;
public B(A myA){
this.abcd = myA::returnTest; // a reference to this method.
}
public void test(){
while(!abcd.getAsBoolean()) { // calls the method each time.
// wait
}
// do something
}
}
If you must do so, one option is to use a java.util.concurrent.atomic.AtomicBoolean, which is mutable type. You can retrieve and update values using get() and set(boolean) respectively. This can also be a good option if thread-safety can potentially be a concern in your application.
Otherwise you have to store a reference to the A instance and use the flag inside it instead of checking a different variable.

override static method functionality

We have class library as
class A {
public void callWorkflow() {
B b = new B();
}
}
class B {
public void callStatic() {
C.someMethod();
}
}
class C {
public static someMethod() {}
}
We are actually trying to change functionality of static method someMethod. Is there a way to solve this problem without changing call hierarchy?
You can't just Override a static method. In my opinion, remove static from the method someMethod(), then create an object of class C inside class B. Then call the method.
Class A{
public void callWorkflow() {
B b = new B();}
}
Class B{
public void callStatic(){
C c = new C();
c.someMethod();}
}
Class C{
public someMethod(){}
}
There is no way to override a static method.
That's why one of these approaches is preferred to calling static methods:
Inject another object (service) that will provide the functionality in a non-static method and call it through the injected object
Make the static method a thin wrapper that just delegates the work to some non-static object that can be configured (like in slf4j's logger)

Java method simulation

As part of behavioral testing I need to simulate the method call in a given class.
So say if I have a class like:
class A {
public void abc(){
new classB().getData();
}
public void xyz( boolean callabc){
if (callabc) {
abc();
}
}
So my requirement is to first find all methods in class A then find which method is making which call like xyz is calling abc which is calling class B getData method.
Is it possible to get all these data in Java?
You can transform your code into something like this:
public class A {
private final ClassB b;
public A(ClassB b) {
this.b = b;
}
public void abc(){
b.getData();
}
public void xyz( boolean callabc){
if (callabc) {
abc();
}
}
}
Then you can pass mock implementation of class B during tests. Then you can verify that some method was invoked on mock with specific parameters with mockito library:
http://static.javadoc.io/org.mockito/mockito-core/2.3.0/org/mockito/Mockito.html#1

Object class with anonymous type constructor

I am creating an Object as a class variable with anonymous type. There are no compilation errors. My question is how to use the class? How to call the methods that I am defining? Where is it used actually?
public class MyClass {
Object o = new Object(){
public void myMethod(){
System.out.println("In my method");
}
};
}
I am not able to call the myMethod() of object o. How to do that and when do we use this?
The only way to call a method of an anonymous class that is not part of the super class methods is to call it straight away:
new Object(){
public void myMethod(){
System.out.println("In my method");
}
}.myMethod();
If you just store the anonymous class in an Object variable you won't be able to call its method any more (as you have figured out).
However the usefulness of such a construct seems quite limited...
To do something like this, you should be having a method in Object class. This in short means you need to override the method defined in Object class.
Try something like:
Object o = new Object(){
public boolean equals(Object object){
System.out.println("In my method");
return this == object;//just bad example.
}
};
Object o2 = new Object();
System.out.println(o.equals(o2));will also print "In my method"
Your variable type is Object, so the only methods that the compiler will let you call are the ones declared in Object.
Declare a non-anonymous class instead:
private static class MyObject {
public void myMethod() {
System.out.println("In my method");
}
};
MyObject o = new MyObject();
You can use interfaces:
public interface MyInterface {
public void myMethod();
}
In your MyClass
public class MyClass {
MyInterface o = new MyInterface() {
#Override
public void myMethod() {
System.out.println("In my method");
}
};
public void doSomething() {
o.myMethod();
}
}

Calling method in object from object within it

I have the following problem, and no idea how to solve it. Let's say we have a class classA, and in this class is the main method creating an object of the class itself. Now we take another class, classB. In the constructor of classA we make an object of classB. Now in a method of classB we want to call a method of classA.
Let me provide you with an example.
public class classA {
public classA() {
//some code
classB objectB = new classB();
}
public static void main(String[] args) {
classA objectA = new classA();
//more code
}
public void methodA() {
//even more code
}
}
public class classB {
public void someListener() {
//code needed to call methodA of the object objectA
}
}
The question is: what would the code be, where there is now just the comment //code needed to call methodA of the object objectA?
The reason I'm in this situation, is that in the code that I'm going to use it for, there are various methods running in objectB controlled by loops, but once a certain thing happens a method in what is shown here as objectA has to be called. How do I do this?
Simple ... just pass the reference of class A object to object of class B. Let class B store the reference and call the methods of A whenever necessary.
New Code (commented)
public class classA{
classA{
//some code
classB objectB = new classB(this); //------------- pass reference here
}
public static void main(String[] args){
classA objectA = new classA();
//more code
}
public void methodA(){
//even more code
}
}
public class classB{
private classA storedReference = null; //------------- you need a variable of classA to store the reference
public classB(classA passedObject){ //------------- you need an appropriare constructor
storedReference = passedObject; //------------- store the classA object reference
}
public void someListener(){
storedReference.methodA(); //------------- call methods whenever necessary
//code needed to call methodA of the object objectA
}
}
You can pass a reference to ClassA to the constructor in ClassB e.g
class ClassB(ClassA ref) {
ref.someMethod();
}
However you need to be careful if you're calling a method on an instance of a class that you're constructing. The class may not have completed its construction and may be be in an incomplete state.
You need to add constructor to your classB, so that you can pass a classA as reference to it.
So your classB becomes:
public class classB {
classA cA;
public classB(classA cA) {
this.cA = cA;
}
public void someListener(){
cA.methodA();
}
}
And your classA constructor :
classA{
//some code
classB objectB = new classB(this);
}
In ClassB, you want to call a method of ClassA. That's not difficult to do, but I believe you have your relationships incorrect.
If you're using the current code you have: You need to pass an instance of ClassA to ClassB, which can be accomplished using the this operator:
public classA() {
//some code
classB objectB = new classB();
objectB.someListener(this);
}
In classB:
public void someListener(classA theClassA) {
//code needed to call methodA of the object objectA
theClassA.methodA();
}
If you want to reverse the relationships between the two: Instead of having an instance of classB in classA that relies on classA to do its work, have classB be the dominant object, and have it contain and instance of classA.

Categories