Alter property of an object used inside the called method in Java

javascript object function
javascript object properties
java object
how to call a variable from another method in java
how to call variable from one method to another method in java
how to pass value from one method to another in java
call function inside object javascript
how to use return value in another method java

I have a java, spring web application using maven as build mechanism. Consider the following code (over-simplified version of my situation) where a controller calls a service to perform some operation and it turn calls some DAO methods to perform some actions in DB.

class MyController extends Controller {
    public ModelAndView handleRequest(... request) {
        boolean performCheck = Boolean.valueOf(request.getParameter("doCheck"));
        myService.doSomeAction(object)
        return ...; 
    } 
}

class MyService {
    public void doSomeAction(Object o){
        myDao.doSomething(o);
    }
}

class MyDao exterds HibernateDaoSuppot {
    boolean check;

    public void doSomething(Object o){
        if(check == true){
            // some action
        } else {
            // some other action
        }

    }
}

My question is how can I alter the value of the check boolean in the Dao method, based on the value I receive in the controller without explicitly passing the boolean through all the layers? I work with a legacy code with lot of business logics and the business team is not confident of making too many modifications to the existing code. However I am free to add any no of classes or aspects to perform the same.

I have tried reading the call stack in the DAO method and determine the boolean several layers above, but I do not feel good about working with the call stack and I am afraid that some future changes in the app architecture or JVM changes can mess up the call stack.

Of course the best is to pass the boolean variable. but instead; you can declare two functions, one for perfromCheck == true and another one for performCheck == false and call either from the controller based on the value of performCheck.

Modifying Object Properties, By calling the object's SetProperty method with a property-value pair: to modify a Boolean property; you will need to use the dot operator or call the object's However, if inside of a method you get or set a property of the self object, IDL will access the Nondrawable Java Export Example · Appendix - IDL Java Object API. Java does not support “directly” nested methods. Many functional programming languages support method within method. But you can achieve nested method functionality in Java 7 or older version by define local classes, class within method so this does compile.

You can add a transient(transient only if object is a persistent object) boolean field in your object.

Set the value of the boolean in your controller before passing the object to your service. You wont even need to maintain a separate check variable in your DAO layer.

Your controller might look like:

class MyController extends Controller {
    public ModelAndView handleRequest(... request) {
        object.setPerform(Boolean.valueOf(request.getParameter("doCheck"))); // object has a boolean field named 'perform'
        myService.doSomeAction(object)
        return ...; 
    } 
}

Your MyDao might look like:

class MyDao exterds HibernateDaoSuppot {
    //boolean check; wont need this

    public void doSomething(Object o){
        if(o.getPerform() == true){
            // some action
        } else {
            // some other action
        }
    }
}

4. Methods Use Instance Variables: How Objects Behave, Methods Use Instance Variables: How Objects Behave State affects behavior, behavior affects Head First Java, 2nd Edition by Kathy Sierra, Bert Bates The method can't change the bits that were in the calling variable x. and registered trademarks appearing on oreilly.com are the property of their respective owners. String property = this.property; PHP: $property = $this->property; or would you do: Java: String property = this.getProperty(); PHP: $property = $this->getProperty(); Forgive me if my Java is a little off, it's been a year since I programmed in Java

As your case: "check" is property of MyDao, you want change it dynamically from a method scope, this may cause Concurrent issues if you use single instance, that's not recommend.

As mentioned by above user, you can use two MyDao instance in your app, one declare with True and the other with False, and your controller determine which one to use.

Object methods, "this", A function that is the property of an object is called its method. If we used this.​name instead of user.name inside the alert , then the code  The java.util.Properties.setProperty(String key,String value) method calls the Hashtable method put. Provided for parallelism with the getProperty method. Enforces use of strings for property keys and values. The value returned is the result of the Hashtable call to put. This method returns the

You could make the boolean in the controller as a private class attribute and then use a getter to access its value from Dao.

Using Objects (The Java™ Tutorials > Learning the Java Language , You may need to use the value of one of its fields, change one of its fields, or call one of its methods to perform an action. So to refer to the origin, width, and height fields within the Rectangle object named rectOne, the CreateObjectDemo​  The object is unreferenced, and its resources are free to be recycled by the Java Virtual Machine. Calling an Object's Methods. You also use an object reference to invoke an object's method. You append the method's simple name to the object reference, with an intervening dot operator (.).

Passing Information to a Method or a Constructor (The Java , The parameters are used in the method body and at runtime will take on the in the computePayment method, and reference data types, such as objects and You can use a construct called varargs to pass an arbitrary number of values to a + x); } // change parameter in passMethod() public static void passMethod(int p)​  Functions that are stored in object properties are called “methods”. Methods allow objects to “act” like object.doSomething(). Methods can reference the object as this. The value of this is defined at run-time. When a function is declared, it may use this, but that this has no value until the function is called.

Parameters are passed by value, but editing them will edit the actual , When you call a method passing an object to it, java does not really pass gets a reference to the local variable used as argument in the call. How to Return Object from a Method in JAVA. A method returns to the code that invoked it when it: Completes all the statements in the method. Reaches a return statement. or Throws an exception (covered later) Whichever occurs first between the last two.

Java Class Attributes, In the previous chapter, we used the term "variable" for x in the example (as shown Create a class called " MyClass " with two attributes: x and y : If you create multiple objects of one class, you can change the attribute The next chapter will teach you how to create class methods and how to access them with objects. Data hiding, which means that critical data stored inside the object is protected from code outside the object, is accomplished in Java by: a. using the public access specifier on the class methods b. using the private access specifier on the class methods c. using the private access specifier on the class definition

Comments
  • Not sure why you need to change value of singleton class field from RestController. This can result in unidentified bugs.
  • @SukhpalSingh Yes, your are right. But I do not have any other option to perform two different actions in DAO layer. As I said, this is a code that I have not written, but I am asked to implement this check without any major code changes
  • If check variable is introduced by you, then create a new object of type CheckObject (please name something better) and use that from your controller layer. Then you can check using instanceof.