I am getting this warning: warning: [static] static method should be qualified by type name, AnchorPane, instead of by an expression
here is my code:
public Chart(Vector<String[]> v, final Pane p, final AnchorPane ap){
super();
this.v = v;
p.heightProperty().addListener(new ChangeListener<Number>() {
public void changed(ObservableValue<? extends Number> ov,
Number old_val, Number new_val) {
draw();
System.out.println(heightProperty().doubleValue()+" "+ap.getBottomAnchor(p));
}
});
}
There is one simple way of solving the non-static variable cannot be referenced from a static context error. Address the non-static variable with the object name. In a simple way, we have to create an object of the class to refer to a non-static variable from a static context.
A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables. Since the static method refers to the class, the syntax to call or refer to a static method is: class name. method name.
By using static variables a single copy is shared among all the instances of the class, and they can be accessed directly by class name and don't require any instance. The Static method similarly belongs to the class and not the instance and it can access only static variables but not non-static variables.
This is what is meant with the warning, “a static method (sayHello) should be accessed in a static way (via the class name Parent or Child).” This convention helps reduce the chance that we've made a coding mistake and it helps clarify that a class method is being called to those who subsequently read our code.
AnchorPane.getBottomAnchor()
is a static method. Static methods are associated with a class, not an instance, and should therefore be called by their class name, not through a reference. The reason is to avoid confusion about which method is finally called, since static methods can not be overridden. See also https://stackoverflow.com/a/2629846/1611055 for some good additional information.
Try
System.out.println(heightProperty().doubleValue()+" "+AnchorPane.getBottomAnchor(p));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With