Java-使用另一个文件中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27127199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:17:12  来源:igfitidea点击:

Java- use a variable from another file

java

提问by user2195411

How can I use a variable from another file? I am writing automation tests (Selenium and TestNG). I want to store some data variables and xpaths in a separate file (secondFile.java)

如何使用另一个文件中的变量?我正在编写自动化测试(Selenium 和 TestNG)。我想在一个单独的文件 (secondFile.java) 中存储一些数据变量和 xpath

Master File:

主文件:

import secondFile.help;

public class TicketAdminTestSuite extends something {

       void printStuff(){
            System.out.println(bar);
} 

}

==============================================

==============================================

Helper file (name: help.java):

帮助文件(名称:help.java):

public class help  {
    public static final String bar = "blah";
}

回答by robin

There are two severe errors in your code:
- the helper file name must be the same as the class name
- the import in the master file must import the helper file with the full package name (I assume the files are in the same package)

您的代码中有两个严重错误:
- 帮助文件名必须与类名相同
- 主文件中的导入必须导入具有完整包名的帮助文件(我假设文件在同一个包中)

// master file TicketAdminTestSuite.java
import Help;
public class TicketAdminTestSuite extends something {

   void printStuff(){
        System.out.println(Help.bar);
   } 
}

// help file Help.java
public class Help  {
    public static final String bar = "blah";
}

回答by Eugene Loy

Simpliest way to do this is to import your helpclass and access it like:

最简单的方法是导入您的help类并像这样访问它:

import help;

...

String test = help.bar;

... or you can use static import:

...或者您可以使用静态导入

import static help.bar;

...

String test = bar;

回答by Sagar Pudi

public classname should match with its file name.

public class名称应与其文件名匹配。

eg:

例如:

First.java

首先.java

import demo.Second;
public class First{
 Second second=new Second();
       void printStuff(){
            System.out.println(second.getBar());
       } 
}

Second.java

第二个.java

 package demo;
    public class Second{
     String bar="blah";
     public String getBar(){
              return this.bar;
     }
    }

回答by Tilion

Just write help.bar:

只需写 help.bar :

   void printStuff(){
        System.out.println(help.bar);

But this example is a bit confusing because the public class must be called the same as the .java file. And if you made a second class in the secoundfile you wouldn't be able to access it from your first file. This would be a better example: import secondFile;

但是这个例子有点混乱,因为公共类必须和 .java 文件一样被调用。如果您在 secoundfile 中创建了第二个类,您将无法从您的第一个文件访问它。这将是一个更好的例子: import secondFile;

public class TicketAdminTestSuite extends something {

   void printStuff(){
        System.out.println(secondFile.BAR);
} 

}

And the second file is made like this

第二个文件是这样制作的

public class secondFile  {
     public static final String BAR = "blah";
}

回答by Sapikelio

Use a getter method.

使用 getter 方法。

  public class help  {
        public static final String bar = "blah";            
        public String getBar(){
              return this.bar;
        }         
    }

And then use it on your code.

然后在您的代码中使用它。

import secondFile.help;

public class TicketAdminTestSuite extends something {
       Help help = new Help();
       void printStuff(){
            System.out.println(help.getBar());
       }    
}