How can I not use argument to pass values between methods of different classes

Saber

In general, value passing between different classes is done with argument of method, For example, passing id value like this:

public class Controller {
    private Service service = new Service();
    void controllerMethod() {
        Integer id = 5;
        service.serviceMethod(id);
    }
    public static void main(String[] args) {
        Controller c = new Controller();
        c.controllerMethod();
    }
}

public class Service {
    private Dao dao = new Dao();
    void serviceMethod(Integer id) {
        dao.daoMethod(id);
    }
}

public class Dao {
    void daoMethod(Integer id) {
        System.out.println(id);
    }
}

These method calls are actually in a single thread, there are all in "main" thread. So I wonder if I can store this value in class 'Controller' in ThreadLocal and then get it from ThreadLocal in class 'Dao', but I failed.

public class Controller {
    private Service service = new Service();

    void controllerMethod() {
        Integer id = 5;
        System.out.println(Thread.currentThread().getName());
        // ----add code
        ThreadLocal<Integer> local = new ThreadLocal<>();
        local.set(id);
        // ----
        service.serviceMethod();
    }

    public static void main(String[] args) {
        Controller c = new Controller();
        c.controllerMethod();
    }
}

public class Service {
    private Dao dao = new Dao();

    void serviceMethod() {
        System.out.println(Thread.currentThread().getName());
        dao.daoMethod();
    }
}

public class Dao {
    void daoMethod() {
        System.out.println(Thread.currentThread().getName());
        // ----add code
        ThreadLocal<Integer> local = new ThreadLocal<>();
        System.out.println(local.get());
        // ----
    }
}

result:

I don't know why...Can this method be used to achieve passing value without argument actually? I hope someone can help me.

ps: Is there any other way to implement a no-arguments transfer of values between multiple classes?

user3151902

A ThreadLocal is usually used as a static variable:

public class Main {
    private static final ThreadLocal<Integer> CURRENT_ID = new ThreadLocal<>(1);

    private void method1() {
        CURRENT_ID.set(100);
        method2();
    }

    private void method2() {
        System.out.println("ID: " + CURRENT_ID.get());
    }
}

But for regular application code, you will almost never need to use ThreadLocal directly. Even if it makes writing the code easier, always keep testing in mind! So in your case, I would consider to keep passing the necessary variables as parameters.

If you considered passing dependencies like DAOs or services via ThreadLocal, you should instead consider dependency injection.

When having method with too many parameters, instead of passing some parameters via ThreadLocal, have a look at parameter objects.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I use same methods in different classes type ArrayList?

From Dev

How can I pass arguments from the console to different methods?

From Dev

How can I use composition with methods in ES6 Classes?

From Dev

Javascript: How can I communicate between different classes?

From Dev

How can I preserve values between WCF service methods calling?

From Dev

How to use generics for the similar methods with different classes

From Dev

How to use generics for the similar methods with different classes

From Dev

How can I use css properties with different classes?

From Dev

How can I pass null values in BETWEEN clause in oracle?

From Dev

How can i use enum to pass variables between two scripts?

From Dev

Can I pass self as the first argument for class methods in python

From Dev

How can I use SASS @for loops to pass values to a Breakpoint include?

From Dev

How can I access methods of different classes in java using the array of type Object

From Dev

How can I access methods of different classes in java using the array of type Object

From Java

How can I pass an argument to a PowerShell script?

From Dev

How can I pass argument with requestAnimationFrame?

From Dev

How can I pass argument with parameter to gulp?

From Dev

how can I pass argument(s) through nose.run that can be picked up by list of python test files containing test methods

From Dev

Is there a way to use methods between classes?

From Dev

Is there a way to use methods between classes?

From Dev

Can I use the same name for different methods in different interface?

From Dev

How to pass session values to different methods in play and slick?

From Dev

How to pass session values to different methods in play and slick?

From Dev

How to pass a parameter to a class between different modules' same classes in java?

From Dev

How can I use Java Native Interface to pass a byte array into a C function which takes a char* as an argument?

From Dev

how to pass values between methods from external class

From Dev

how to pass values between methods from external class

From Dev

How can I avoid dependencies between methods?

From Dev

How can I use `class` as a keyword argument?

Related Related

  1. 1

    How can I use same methods in different classes type ArrayList?

  2. 2

    How can I pass arguments from the console to different methods?

  3. 3

    How can I use composition with methods in ES6 Classes?

  4. 4

    Javascript: How can I communicate between different classes?

  5. 5

    How can I preserve values between WCF service methods calling?

  6. 6

    How to use generics for the similar methods with different classes

  7. 7

    How to use generics for the similar methods with different classes

  8. 8

    How can I use css properties with different classes?

  9. 9

    How can I pass null values in BETWEEN clause in oracle?

  10. 10

    How can i use enum to pass variables between two scripts?

  11. 11

    Can I pass self as the first argument for class methods in python

  12. 12

    How can I use SASS @for loops to pass values to a Breakpoint include?

  13. 13

    How can I access methods of different classes in java using the array of type Object

  14. 14

    How can I access methods of different classes in java using the array of type Object

  15. 15

    How can I pass an argument to a PowerShell script?

  16. 16

    How can I pass argument with requestAnimationFrame?

  17. 17

    How can I pass argument with parameter to gulp?

  18. 18

    how can I pass argument(s) through nose.run that can be picked up by list of python test files containing test methods

  19. 19

    Is there a way to use methods between classes?

  20. 20

    Is there a way to use methods between classes?

  21. 21

    Can I use the same name for different methods in different interface?

  22. 22

    How to pass session values to different methods in play and slick?

  23. 23

    How to pass session values to different methods in play and slick?

  24. 24

    How to pass a parameter to a class between different modules' same classes in java?

  25. 25

    How can I use Java Native Interface to pass a byte array into a C function which takes a char* as an argument?

  26. 26

    how to pass values between methods from external class

  27. 27

    how to pass values between methods from external class

  28. 28

    How can I avoid dependencies between methods?

  29. 29

    How can I use `class` as a keyword argument?

HotTag

Archive