?
-
If I refactor the following class into one below it, should i make the
below nested class static?
public class A {
private int hgap = 0;
private int vgap = 0;
...
public A() {
}
...
public void setHGap(int hgap) {
this.hgap = hgap;
}
...
}
public class A {
private Gap gap = new Gap();
...
public A() {
}
...
public void setGap(Gap gap) {
this.gap = gap;
}
...
public class Gap { <---- to be static or not?
private int hgap = 0;
private int vgap = 0;
public Gap() {
}
...
public void setHGap(int hgap) {
this.hgap = hgap;
}
...
}
}
I'm guessing it should be static so that i can call 'a.setGap(new
A.Gap());'.
If so, under what circumstances should i make it non static and call a
'a.setGap(a.new Gap());' instead? If i were to do this and the
constructor accepts a Gap object as one of the parameter, then it's a
problem since 'a' doesn't exist yet.
Also, if most of the time nested classes are static, can i simply put it
in its own class rather than making it nested if the nested class
does'nt access any of the enclosing class's variables?
below nested class static?
public class A {
private int hgap = 0;
private int vgap = 0;
...
public A() {
}
...
public void setHGap(int hgap) {
this.hgap = hgap;
}
...
}
public class A {
private Gap gap = new Gap();
...
public A() {
}
...
public void setGap(Gap gap) {
this.gap = gap;
}
...
public class Gap { <---- to be static or not?
private int hgap = 0;
private int vgap = 0;
public Gap() {
}
...
public void setHGap(int hgap) {
this.hgap = hgap;
}
...
}
}
I'm guessing it should be static so that i can call 'a.setGap(new
A.Gap());'.
If so, under what circumstances should i make it non static and call a
'a.setGap(a.new Gap());' instead? If i were to do this and the
constructor accepts a Gap object as one of the parameter, then it's a
problem since 'a' doesn't exist yet.
Also, if most of the time nested classes are static, can i simply put it
in its own class rather than making it nested if the nested class
does'nt access any of the enclosing class's variables?