Reputation:
I have an inner bean class in my bean class. In a MyBean class method, I will create an instance of my inner bean class then set some fields value. Please see my sample code.
My question is: Is WAY 1 better performance than WAY 2 because we set value to field directly? Which way we should use? Thank you.
public class MyBean {
public void myMethod() {
String p1 = "calculate p1";
String p2 = "calculate p2";
// WAY 1
InnerBean innerBean = new InnerBean();
innerBean.p1 = p1;
innerBean.p2 = p2;
// WAY 2
InnerBean innerBean = new InnerBean(p1,p2);
innerBean.doSomething();
}
// WAY 1
private class InnerBean {
private String p1;
private String p2;
public void doSomething() {
System.out.println(p1 + p2);
}
}
// WAY 2
private class InnerBean {
private String p1;
private String p2;
public InnerBean1(String p1, String p2) {
this.p1 = p1;
this.p2 = p2;
}
public void doSomething() {
System.out.println(p1 + p2);
}
}
}
Upvotes: 1
Views: 3956
Reputation: 2138
I always use constructors because uninitialized class variables cause lots of bugs. Eclipse, IntelliJ and most other modern IDEs can generate constructors from the list of class variables to save you the trouble of typing it out by hand. I did C++ before Java and the pain caused by incomplete class initialization made me thankful for Java's much clearer class initialization logic. For trivial cases like your private class nothing matters much but I try to keep up best practices always.
Upvotes: 0
Reputation: 879
On the first way, you actually use the default constructor of InnerBean, since it is not declared. This is because java if none constructor has been declared, accepts the use of InnerBean() and it sets the string values for p1 = ""; , p2 = "";
What you actually do afterwards is to make those strings point to something else rather the empty String.
// WAY 1
InnerBean innerBean = new InnerBean();
innerBean.p1 = p1;
innerBean.p2 = p2;
On the second way, the java default constructor is overridden and you pass the values as parameters. I would suggest this way since, you should provide a constructor in your class.
// WAY 2
InnerBean innerBean = new InnerBean(p1,p2);
innerBean.doSomething();
Apart from that, considering the number of steps way 2 requires less. Because in the first case you create the object with predifined values and then change the pointers, while on the 2nd case you just create the object with the given values.
Upvotes: 0
Reputation: 539
I would go with a constructor but this isn't really important in this case if you are not going to move nested class outside ever.
If these fields are immutable then declaring them final and using constructor is the cleanest way to go.
Also, if the nested class doesn't need to reference outer scope (i.e. class MyBean
) you should declare it static
.
Upvotes: 1
Reputation: 2600
I don't think that there will be a significant difference in performance. This is more a question of design and style.
The common way would be to use a constructor or setter and getter. This would fit to the design principle of "information hiding". Since this will be the way that most developer would expect to see, this will also be easier to read for the most of us.
But since the inner class is private I would say it is absolute ok to use direct access to the fields of the inner class, if this is common in your team. This might lead to slender code.
Upvotes: 1