mercredi 23 septembre 2020

How an object can be available to all classes in java?

I am creating an application with many java classes. for keeping the configurable information like username, urls', file paths etc, I am using resources/app.properties file.

For reading values from this file, I am using below code

InputStream input = new FileInputStream("../resources/app.properties");
     
Properties prop = new Properties();

prop.load(input);
     
System.out.println("print the url" + prop.getProperty("username"));

as we can see from the code, for reading any property from app.properties file, we need prop object.

can we write this code in such a way that we just need to write first 3 lines once in the code in the main calss and then we can call prop.getProperty("username") from any class in our whole application?

Aucun commentaire:

Enregistrer un commentaire