简体   繁体   中英

Java- use a variable from another file

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)

Master File:

import secondFile.help;

public class TicketAdminTestSuite extends something {

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

}

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

Helper file (name: help.java):

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

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";
}

Just write 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;

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";
}

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

import help;

...

String test = help.bar;

... or you can use static import :

import static help.bar;

...

String test = bar;

public class name should match with its file name.

eg:

First.java

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

Second.java

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

it was excruciating, but I managed to do it. my code for the first file, "Main.java" is:

import maintu.*;

public class Main extends maintu.Maintu {
    public static void main(String[] args) {
        System.out.println("println");
        System.out.println("I am on a different line");
        System.out.print("I am print ");
        System.out.print("I will be on the same line ");
        System.out.println(3 + 3);
        // I will be ignored
        System.out.println("there is a comment after me"); // so will I
        //System.out.println("I will be ignored too");
        /* I will be ignored until I end


        not over yet.

        Now i am over*/
        System.out.println("I will print though");
        String variable = "you can use me to call on me for later ";
        System.out.println(variable + "and I dont need quotes!");
        int numVariable = 13;
        System.out.print("my number is " + numVariable + ".");
        String hi;
        hi = "This works too!";
        System.out.println(hi);
        int x;
        x = 10;
        x = 20;
        System.out.println(x);
        final String finel = "I cannot change";
        final int que; // I will always be empty. not 0, empty
        int w = 5, y = 6, z = 50; /*you can set several variables in one line like this*/
        /*you can do this as well :
        int x, y, z;
        x = y = z = 50;
        */
        int wholenumber = 1;
        float onepointone = 5.99f;
        char onesinglecharacter = 'D';
        boolean truefalse = true;
        String unlimitedwords = "string = words";
        byte onezero = -13;
        short hexagecimal = -13000;
        long isverylong = -4775808;
        double bdoublo;
        x=0;
        System.out.println(maintu.Maintu.maybe);/*
        while(true) {
            x = x + 1;
            System.out.print(x + " ");
        }*/
    }
}

and my code for the, as you call it "helper" file, "maintu.java" is:

package maintu;
    public class Maintu {
        public static String maybe = "extends";
    }

the way I did it was: write code for files in command prompt: run javac Maintu.java in command prompt: run javac Maintu.java in command prompt: run javac Main.java in command prompt: run java Main.java command prompt output:

println
I am on a different line
I am print I will be on the same line 6
there is a comment after me
I will print though
you can use me to call on me for later and I dont need quotes!
my number is 13.This works too!
20
extends

I am using java 17

Use a getter method.

  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());
       }    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM