Reputation: 63
I want some code (for example, "setBackground(Color.BLACK);") to be executed within every overloaded constructor of this subclass (or I can also mean "some code" must be executed after instance of this subclass is created). But this "some code" can be used/called only inside our subclass and only once. And every overloaded constructor of this subclass must always call appropriate overloaded constructor of the superclass.
public class JLProgressBar extends JProgressBar{
public JLProgressBar(){
super();
}
public JLProgressBar(int orient){
super(orient);
}
public JLProgressBar(int min, int max){
super(min,max);
}
public JLProgressBar(int orient, int min, int max){
super(orient,min,max);
}
public JLProgressBar(BoundedRangeModel newModel){
super(newModel);
}
}
Upvotes: 0
Views: 74
Reputation: 723
You could use an instance initializer. This initializer can handle or your required "some code" for all constructor variants.
public class JLProgressBar extends JProgressBar{
public JLProgressBar(){
super();
}
public JLProgressBar(int orient){
super(orient);
}
public JLProgressBar(int min, int max){
super(min,max);
}
public JLProgressBar(int orient, int min, int max){
super(orient,min,max);
}
public JLProgressBar(BoundedRangeModel newModel){
super(newModel);
}
// instance initializer (called for every constructor
{
// "some code"
}
}
Note: this instance initializer will run after the super constructor but before any other code in the constructor. So
public class Bar
{
protected int a;
public Bar()
{
a = 2;
System.out.println(a);
}
{
a = 1;
System.out.println(a);
}
}
public class Foo extends Bar
{
public Foo()
{
super();
a = 4;
System.out.println(a);
}
{
a = 3;
System.out.println(a);
}
}
public static void main(String[] args)
{
new Foo();
}
... will print
1
2
3
4
Upvotes: 0
Reputation: 3628
Converting comment to an answer for future reference.
Java's Initialiser blocks should resolve the issue. These are injected into each constructor directly after the call to super
, which (in the examples you've provided) should cover happening at the appropriate times. Additionally, these will only run once, and are guaranteed to run for every instance which gets created (regardless of if someone subclasses your class, etc.).
For example
public class YourJProgressBar extends JProgressBar {
public YourJProgressBar() {
super();
}
{
// Whatever code you want in here, will run immediately after the call
// to 'super' in any given constructor. Note, in cases where the call
// to 'super' is implicit, it will run after that implicit call
// instead of just never running.
this.setValue(10);
}
}
Upvotes: 1
Reputation: 638
Try this. static
block will be called each time the class is loaded.
public class JLProgressBar extends JProgressBar{
static {
System.out.println("hhh");
}
public JLProgressBar(){
super();
}
public JLProgressBar(int orient){
super(orient);
}
public JLProgressBar(int min, int max){
super(min,max);
}
public JLProgressBar(int orient, int min, int max){
super(orient,min,max);
}
public JLProgressBar(BoundedRangeModel newModel){
super(newModel);
}
}
Upvotes: 0