I would like to add method through my constructor and use it after :
RegularAxis lon = new RegularAxis(){
public String returnHello(){
return "hello";
}
};
lon.returnHello();
I cannot access my new method. is there an other way?
I would like to add method through my constructor and use it after :
RegularAxis lon = new RegularAxis(){
public String returnHello(){
return "hello";
}
};
lon.returnHello();
I cannot access my new method. is there an other way?
You can call it as part of the same statement:
new RegularAxis(){
public String returnHello(){
return "hello";
}
}.returnHello();
Or you can capture the anonymous type with a var variable in Java 10+ (thanks @Lesiak):
var lon = new RegularAxis(){
public String returnHello(){
return "hello";
}
};
lon.returnHello();
Otherwise, you'll have to declare it as a proper class:
class IrregularAxis extends RegularAxis {
public String returnHello(){
return "hello";
}
}
IrregularAxis lon = new IrregularAxis();
lon.returnHello();