2

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";
}
3
  • 3
    I think your question is a bit confusing. E.g. you cannot have a public class help in a file called secondFile.java You also confuse packages with filenames. Commented Nov 25, 2014 at 12:51
  • For what you are after, you should probably use an enum, and not a class. Commented Nov 25, 2014 at 12:53
  • @Tomas, I will rename the helper file now. Commented Nov 25, 2014 at 12:58

6 Answers 6

5

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";
}
Sign up to request clarification or add additional context in comments.

1 Comment

I renamed the second file above. Yes, I understand. I overlooked that as I was mocking up an example to post here. In regards to your second point, yes same package.
3

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

Comments

2

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;

3 Comments

or import staticly and then you can access bar without help.bar
Thank you @EugenyLoy but if my help file has 100 variables, how could I could I use all 100, without doing import static Ids.variable 100 times?
@user2195411 you can use wildcards. Like: import static help.*;. In this case you'll import everything from help and won't have to specify every variable manually.
1

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

Comments

0

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

1 Comment

It is a whole lot of unneeded code. Also the question was answered in the same way before so this answer doesn't add any useful information.
-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.