Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does error mean? warning: [static] static method should be qualified by type name, AnchorPane, instead of by an expression

Tags:

java

javafx

pane

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));

        }
    });
}
like image 237
user1958884 Avatar asked Jan 21 '13 19:01

user1958884


People also ask

How do you fix non-static method Cannot be referenced from a static context?

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.

How do you call a method in a static way?

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.

How do you access a static variable?

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.

Should be accessed in static way?

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.


1 Answers

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));
like image 190
Andreas Fester Avatar answered Sep 16 '22 22:09

Andreas Fester