Sonar: Make the enclosing method static or remove this set - java

I was trying to access the non static method from class A to a static method in class B
#Component
public class B{
private static A aA1;
#Autowired
private A tA;
#PostConstruct
public void init(){
//<<<Make the enclosed method Static or remove this set>>>
B.aA1=tA;
}
public static void random method(){
aA1.doStuff();
}
}
Is it okay to make the init method static, as the sonar suggest
Or is there another way i could solve this?

Related

Stubing a static private method inside a Utils class with powermockito

I'm having the following flow:
manager.getObject.doSomthing();
Where doSomething() calls a static function from a Utils class, that in turn, calls a private static function e.g:
public class obj {
public void doSomething(){
Utils.do();
}
}
public class Utils {
public static void do(){
int test = doPrivate();
...
~do unrelated computation~
...
}
private static int doPrivate(){
return someComplexMethod();
}
}
And I would like to mock the doPrivate, so I would still be able to test the do() method
Any way to achieve it with powermockito?
Using powermock-api-mockito you can achieve this.
You can mock specific static method of a class. Below is the syntax:
import static org.powermock.api.support.membermodification.MemberMatcher.method;
import static org.powermock.api.support.membermodification.MemberModifier.stub;
stub(method(Utils.class, "doPrivate")).toReturn(response);

why this weird order of constructor/static initializer/static member function in java?

public class DataFactory {
private static DataFactory ourInstance = new DataFactory();
static {
System.out.println("static initialize");
}
private DataFactory() {
System.out.println("constructor");
}
public static void doNothing() {
System.out.println("inside doNothing");
}
}
public class App {
public static void main(String[] args) {
System.out.println("main start");
DataFactory.doNothing();
}
And After I run it, here is the printed sequence:
main start
constructor
static initialize
inside doNothing
Why calling DataFactory.doNothing() will trigger Constructor?
and why constructor is running before the static initializer?
When the class is initialized, it'll execute all of the static {...} and static field initializers, in the order they appear in the code (see JLS 12.4.2, and in particular step 9 in the list of steps there). In your example, there are two such initializers:
private static DataFactory ourInstance = new DataFactory();
The static {...} block
So, the first one happens first. It instantiate an object and assigns its reference to ourInstance. To instantiate the object, it needs to call the constructor, which it does (as you saw).
When that's done, the static block is executed, which prints "static initialize."
At this point, the class is initialized, and the method doNothing can finally be invoked.
It's invoking the constructor because you are creating an instance of DataFactory inside the same DataFactory; so it needs to call the constructor once in order to be able to instantiate it. Comment or delete the private static DataFactory ourInstance = new DataFactory(); line and the constructor call is not going to happens.
A static initializer is executed right after the class is initialized.
Sorry to tell that Rod_Algonquin is incomplete and Machina too. The right answer is that:
"Static Initialization Blocks run when the class is first loaded"
So, you are correct to ask "why the constructor is run before static?!".
How can? And there is a rule for initialization blocks (statics and instance).
The order in which initialization blocks appear in a class matters.
Just swap the order of your static initialization block to the static instantiation to see what happen:
public class DataFactory {
static { //// SWAPPED HERE
System.out.println("static initialize");
}
///// SWAPPED HERE
private static DataFactory ourInstance = new DataFactory();
private DataFactory() {
System.out.println("constructor");
}
public static void doNothing() {
System.out.println("inside doNothing");
}
}
OUTPUT:
main start
static initialize
constructor
inside doNothing
Your static field initialization calls the constructor.
In the end, the code that initializes the field "ourInstance" is also part of the static initializer.
So what is actually happening is:
public class DataFactory {
private static DataFactory ourInstance;
static {
outInstance = new DataFactory(); // 2
System.out.println("static initialize"); // 4
}
private DataFactory() {
System.out.println("constructor"); // 3
}
public static void doNothing() {
System.out.println("inside doNothing"); // 6
}
}
public class App {
public static void main(String[] args) {
System.out.println("main start"); // 0
DataFactory.doNothing(); // 1 (static init) and 5 (method call)
}
}
Here is the JLS documentation for the constructor:
Constructors are invoked by class instance creation expressions (§15.9)
And the JLS documentation for the static block:
A static initializer declared in a class is executed when the class is initialized (§12.4.2).
As you can see the constructor is first called when the class is initialized and then right after the constructor call then the static block is then called.

Why can't we access methods of non-public classes inside a java file?

class Basics415 {
public static void main_hooo(){
out.println("1234");
}
void main_ho(){
}
}
In another file called Basics5.java:
public class Basics5 extends Basics415{
public static void main(){
main_hooo(); // We are accessing a public method of Class Basics415
main_ho(); // BUT WE CANNOT ACCESS A NON PUBLIC METHOD FROM SAME CLASS
}
}
Why can't we access main_ho() while we can access main_hooo() ?
Why basic415.main_ho or Basic415.main_hooo doesn't work inside the main method of Basics5?
Because the methods are static and therefore the classes need are needed to access them.
Basics4.method_Inside_Basics4()
So, after you edited a bunch of code ...
public class Basics5 extends Basics415{
public static void main(){
// accessing a static method in a static context.
Basics415.main_hooo();
// accessing an instance method in a static context.
final Basics415 b = new Basics415();
b.main_ho();
}
}
Your trouble is that the main metod of class Basics5 is a static method, static methods are defined at class level and not at instance level, then you can't use method not static in a static method.
You should access method_Inside_Basics4 method as Basics4.method_Inside_Basics4(). As Basics5 class extends Basics415 class, it inherits main_hooo method, which can be assessed from Basics5.

calling non static method from static class in java with spring autowiring?

I have below interface and its implementation class.
Demo.java
public interface Demo{
void showDemo();
}
DemoImpl.java
#Service
public class DemoImpl implements Demo{
public void showDemo(){
//To Do
}
}
Now i have one class with static method which will internally call showDemo() as below.
DemoStatic.java
#Component
public class DemoStatic{
#Autowired
private Demo demo;
public static void callShowDemo(){
demo.showDemo(); //calling non static method from static method
}
}
Here i am calling non static method from static method. Is my design correct? Or do i need to change my design? Please suggest me.
Thanks!
You can do it this way
#Component
public class DemoStatic {
private static Demo demo;
#Autowired
public void setDemo(Demo d) {
demo = d;
}
public static void callShowDemo(){
demo.showDemo(); //calling static method from static method
}
}
No your design is not correct.
private Demo demo;
has to be
private static Demo demo;
You just cant use NON static members in a static method
The above code will not compile at all. You are trying to refer non-static reference from static method, which is not allowed in Java.
Refer this stackoverflow link for better understanding.
Make the following change:
public static Demo demo;
when you call DemoStatic.callShowDemo(),the demo may not been instand

Get past static field of jUnit:s #BeforeClass

Is there any way around the classic problem of
public class SomeClass implements SomeOtherInterface {
private static SomeInterface some;
public SomeClass(SomeInterface someInterface) {
some = someInterface;
}
#BeforeClass
public static void doSomethingWithInterface() {
System.out.println(someInterface.someValue()); // prints null
}
}
other than exchanging
System.out.println(someInterface.someValue()); // prints null
with
System.out.println(SomeInterface.someValue());
if someValue is static. The problem is that this is for an framework (extension), and and an implementation of SomeInterface is to be provided by the user.
You set the value of the static member just in the constructor. So before not having at least one object of that class, you won't be able to access someValue(). In Junit the #Before annotation might be useful which is executed before each test and is not static.

Categories

Resources