How to set a double method equal to a value in a static method?

I have a method called getX1() which gets data from a different class:

    public double getX1(){
    double x1 = getIntent().getExtras().getDouble("s_xd2");

    return x1;
}

and I want to set this equal to a value in a static method.

    public static double[] xWerte() {
    double x1 = getX1();

    return new double[] {x1,2.0,3.0,4.0,5.0,6.0,7.0};
}

But it won't let me... How does it work?

It says: Non-static method 'getX1()' cannot be referenced from a static context

Jon Skeet
people
quotationmark

It says: Non-static method 'getX1()' cannot be referenced from a static context

Right - that's got nothing to do with creating an array, or anything like that.

The problem is that getX1() is an instance method - it needs to operate on an instance of the declaring class. Your xWerte method is a static method, so it doesn't naturally have an instance to operate on.

Options:

  • Make xWerte an instance method too
  • Give xWerte an instance of the class to work with (e.g. passing it in as a parameter)
  • Make getX1 a static method (probably tricky, given the getIntent call)

people

See more on this question at Stackoverflow