开发者

how do I override a static variable of a class

I'm using a library it has a public static float variable I want to know how to override it I gue开发者_运维技巧ss that's not going to happen?

//Settings.java
package org.jbox2d.common;

public class Settings {
    public static float velocityThreashold = 1.0f;
}

//MyClass.class
package org.jbox2d.common;

import com.otherlibrary
public class MyClass {

}

Thanks


You can not override member variables in Java. You can use something called field hiding instead. Have a look at this.

However in your example velocityThreashold isn't final so you can change it's value.


The velocityThreashold variable in your example is not final, nor is it an instance variable and therefore cannot technically be overridden.

What you can do is set the value of velocityThreashold to any value you want since it's public.

I think what you're going to want to do is something like the following:

public static void main(String[] args) {
  org.jbox2d.common.Settings.velocityThreashold = 2.0f;

  //... the rest of your program
}


Override variable? If you mean method then:

You can't override static method because static elements isn't inherited


You can't, that's the point of them being final.

As for you code you don't have a final variable so you just have to change the value and that's it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜