Wednesday, June 12, 2013

How to share a variable between classes

In this post, I will explain how to share a variable between classes.

first, you need a seperate class that contains variables you want to share.
I will name this class "DataContainer"

package test_var;

/**
 *
 * @author Lee
 */
public class DataContainer {
    
    private int[] lvl = {1, 2, 3};

    /**
     * @return the lvl
     */
    public int[] getLvl() {
        return lvl;
    }

    /**
     * @param lvl the lvl to set
     */
    public void setLvl(int[] lvl) {
        this.lvl = lvl;
    }
    
    // for chaning lvl value one by one
    public void setLvl(int index, int value) {
        this.lvl[index] = value;
    }

}



Now, lets make two classes that shares the data from DataContainer.
I will name the classes "ClassOne" and "ClassTwo."

Here is the ClassOne
package test_var;

/**
 *
 * @author Lee
 */
public class ClassOne {
    
    public ClassOne(DataContainer data) {
        
        // First, display the original data in the DataContainer
        for(int i = 0; i < data.getLvl().length; i++) {
            System.out.println("Class One Lvl display :" + data.getLvl()[i]);
        }
        
        // Second, change the data in the DataContainer.
        for(int i=0; i < data.getLvl().length; i++) {
            data.setLvl(i, data.getLvl()[i] + 10);
        }
    }
}

And this is ClassTwo
package test_var;

/**
 *
 * @author Lee
 */
public class ClassTwo {
    
    public ClassTwo (DataContainer data) {
        // First, display the original data in the DataContainer
        for(int i = 0; i < data.getLvl().length; i++) {
            System.out.println("Class Two Lvl display :" + data.getLvl()[i]);
        }
        
        // Second, change the data in the DataContainer.
        for(int i=0; i < data.getLvl().length; i++) {
            data.setLvl(i, data.getLvl()[i] + 20);
        }
        
        // then print it
        for(int i = 0; i < data.getLvl().length; i++) {
            System.out.println("Class Two Lvl display :" + data.getLvl()[i]);
        }
    }
    
}        

Brief explanaion of the two Classes

ClassOne gets the origianl data from DataContainer and displays it.
Then it adds 10 to the original data.

ClassTwo gets the data and displays it.
Then it adds 20 to the original data, then displays it.

Here is the main method.

package test_var;

/**
 *
 * @author Lee
 */
public class Display {
    public static void main(String[] args) {
        
        // call the original lvl data = {1, 2, 3}
        DataContainer container = new DataContainer();
        
        // print original lvl data and adds 10 to each of the data
        ClassOne classOne = new ClassOne(container);
        
        // get the changed lvl data and print it. 
        // Then adds 20 to the value and print
        ClassTwo classTwo = new ClassTwo(container);
    }
}

In the main method, ClassOne gets the original data, and modifies it.
Then the ClassTwo get the modified version of data, and changes it again.

Here is the output of the main method.
Class One Lvl display :1
Class One Lvl display :2
Class One Lvl display :3
Class Two Lvl display :11
Class Two Lvl display :12
Class Two Lvl display :13
Class Two Lvl display :31
Class Two Lvl display :32
Class Two Lvl display :33

as you can see, ClassTwo prints 11, 12, 13 since it gets data from ClassOne, then the data becomes 31, 32, 33 after ClassTwo modifies it

No comments:

Post a Comment