how do I override a static variable of a class

放肆的年华 提交于 2019-12-13 09:57:21

问题


I'm using a library it has a public static float variable I want to know how to override it I guess 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


回答1:


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
}



回答2:


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.




回答3:


Override variable? If you mean method then:

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




回答4:


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.



来源:https://stackoverflow.com/questions/5397082/how-do-i-override-a-static-variable-of-a-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!