简体   繁体   English

如何从main方法(或任何方法)中的args []初始化java中的常量,例如“ public static final Integer”

[英]How to initialize constant such as “public static final Integer” in java from args[] in main method (or any)

I'm basically a beginner in Java and I am trying to figure out this problem: 我基本上是Java的初学者,并且我试图找出这个问题:

my project uses (integer)constant through many classes and I need to set up this constant from file/argument of programm and I have no idea how. 我的项目通过许多类使用(整数)常量,我需要从programm的文件/参数中设置此常量,但我不知道如何。 I could remove "final" statement but that is against all conventions. 我可以删除“ final”语句,但这违反所有约定。

How to resolve this? 如何解决呢? What is best way to avoid it? 避免这种情况的最佳方法是什么? Please help me :) 请帮我 :)

short example: 简短的例子:

public class App {
    public static final int k;  
    public static void main( String[] args ) {
        k = Integer.parseInt(args[0]); // does not work ... sure but how? 
    }
}

EDIT: public STATIC void main ... (it was missing static) 编辑:公共静态无效主...(它缺少静态)

You can only use this kind of functionality within the static { } block. 您只能在static { }块中使用这种功能。

I'd recommend making the constant itself private, and accessible only through public static getter methods. 我建议将常量本身设为私有,并且只能通过public static getter方法访问。 This should be a suitable architecture. 这应该是一个合适的体系结构。

You need to use a static{} block: 您需要使用static{}块:

public class App {
    public static final int k;

    static {
        String kvalue = "";
        try
        {
            // Insert some code to open a file and read a value from it.
            kvalue = "<value from file>";
        }
        catch( Exception e )
        {
            // handle any exceptions opening the file
        }
        finally
        {
            k = Integer.parseInt( kvalue );
        }
    }

    public static void main( final String[] args ) {
        // do stuff
    }
}

Alternatively, you could have each class that requires this value access to a shared pool of data, the ApplicationContext , where the value X would reside as a constant: 或者,您可以让每个需要此值访问共享数据池ApplicationContext ,其中值X将作为常量驻留在其中:

public final class ApplicationContext {

    public static abstract class Worker {

        private final ApplicationContext mApplicationContext;

        public Worker(final ApplicationContext pApplicationContext) {
            this.mApplicationContext = pApplicationContext;
        }


        public final ApplicationContext getApplicationContext() {
            return this.mApplicationContext;
        }

    }

    private final int mX;

    ApplicationContext(final String[] pArgs) {
        this.mX = pArgs[0];
    }


    public final int getX() {
        return this.mX();
    }

}

By having all relevant classes extend ApplicationContext.Worker , we can ensure access to the constant value without having to rely upon a static implementation because all classes will receive a reference to ApplicationContext upon construction. 通过使所有相关的类扩展ApplicationContext.Worker ,我们可以确保访问常量值而不必依赖static实现,因为所有类在构造时都会收到对ApplicationContext的引用。

它不起作用,因为static final变量只能在静态块中初始化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Java 中调用公共 static void main(String[] args) class 中的方法? - How to call a method inside the public static void main(String[] args) class in Java? 对于 Java 接口中的常量来说,“public static final”是多余的吗? - Is “public static final” redundant for a constant in a Java interface? 对main方法的误解/“ public static void main(String [] args)”的观点 - Misunderstanding main method/the point of “public static void main(String[] args)” java Hello World 错误:在类名中找不到主方法,请将主方法定义为:public static void main(String[] args) - java Hello World Error: Main method not found in classname, please define the main method as: public static void main(String[] args) 如何根据 cmd 参数初始化 static 最终变量? - How to initialize static final variables based on cmd args? 如何从java中的其他类初始化静态最终变量 - how to initialize static final variable from other class in java 什么是Java中的“公共静态最终”常量的Clojure等价物 - What is the Clojure equivalent of a “public static final” constant in Java 如何从 Scala 访问 Java 类中的公共静态最终成员? - How to access a public static final member in a Java class from Scala? 公共void main(String [] args)是无效的Java main方法签名吗? - Is public void main(String[] args) Invalid java main method signature ? 使用 public static void main(String args[]) 从子 class JAVA 获取 output - Using public static void main(String args[]) to get output from child class JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM