'how the same Instance work on different method on another class in java
Same instance is used in multiple method based on the argument providing. can we consider thread safety here. Let's assume that we've 2 class, Class Main & Class demo.
Class Demo {
returnType methodExample(args...) {
//based on the argument provide it returns
return something
}
}
Class Main {
Demo demo = new Demo();
returnType method1() {
value = demo.methodExample(args);
}
returnType method2() {
value = demo.methodExample(args);
}
}
will it share same Demo Instance. And what of both method works asynchronously. please also describe the case in Spring Injecting by @AutoWired annotation
Apologies for typos or other mistakes.
Solution 1:[1]
yes it will share same Demo instance. but the calculations are performed under method methodExample() is based on what value you are passed on parameters. for using @autowire first you have to use @component annotation on Demo Class.
@Component
Class Demo {
returnType methodExample(args...) {
//based on the argument provide it returns
return something
}
}
Class Main {
@Autowired
Demo demo;
returnType method1() {
value = demo.methodExample(args);
}
returnType method2() {
value = demo.methodExample(args);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Akash kumar |
