简体   繁体   中英

Import variable for class ato another class

I want to transfer a variable from a class FenJeu to another class FenInfos . The variable butrouge and butbleu should go from FenJeu to a class called FenInfos .

FenJeu class :

public class Joueurs extends JLabel {
        public int butrouge = 0;
        public int butbleu = 0;

            if(xba < 130 && yba > 447 && yba < 607) {
                butbleu = butbleu + 1;
            }
            if(xba > 1564 && yba > 447 && yba < 607) {
                butrouge = butrouge + 1;
             }

Should I put the variables in a public field ?

As far as my understanding of your question, you want to access your variables from another class.

You can try importing your class and then using getter functions you can fetch those variables into any classes you want. Either use inheritance or import that class and call a function from that class which will fetch that value and save it into this immediate class.

Reference Link for Importing

PS - FenJeu class - The code that you put up there wont work unless you put those condition inside a function.

Lets assume this is your first class and other class also is in the same package

package testDataTrial;

public class TestDate { 

public int test = 0;

public void dummyMethod() {
    // method content
}

In the second class, you can create an object for the class. of which you ant to access the variables. and then acess the variable by doing Object.Variable / Method

package testDataTrial;

public class TestTwo {


public static TestDate td = new TestDate();

 public static void main(String[] args) {
    // TODO Auto-generated method stub
    td.test = 1;
    td.dummyMethod();
 }
}

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