[Solved] How to import a String from a a different Class


This has to do with the scope of your variables. You can find more informatnion here.

Currently the variables stored in class A have package-private scope. There are really two ways to do what you are describing.

The better solution would be to provide a getter method within classA:

public String getA(){
   return this.A;
}

This will access the A variable within the instance of the classA class. You can then change your main() to the following:

public static void main(String[] args){
    classA newclassA=new classA();
    String Z= newclassA.getA(); // Z = "TextL: THIS IS THE STRING";
}

Another option is to change the scope to protected to allow subclasses to access the variable field directly. i.e.

class classA{
    protected String A="THIS IS THE STRING";
    private String B="TEXTL: ";

    public classA(){
        this.A=B+A;
    }   
}

and

public static void main(String[] args){
    classA newclassA=new classA();
    String Z= newclassA.A; // Z = "TextL: THIS IS THE STRING";
    // this allows you to access fields as if you were in the actual classA class.
}

solved How to import a String from a a different Class