Java Passing variables around classes

Posted by nazerb on Stack Overflow See other posts from Stack Overflow or by nazerb
Published on 2013-10-25T09:51:42Z Indexed on 2013/10/25 9:53 UTC
Read the original article Hit count: 215

Filed under:
|

I am new to java and am trying to pass variables like in the following example from one class to another, im wondering is this possible and how i would go about it if it is.

As this code does not work as it is not static.

Main Class

public class testAll
{
    public static void main(String[] args)
    {
        One one = new One();
        Two two = new Two();


    }
}

The first class:

public class One
{

    public int test = 4;

    public int getTest()
    {
        return this.test;

    }


}

The second class:

public class Two
{

    public void value()
    {
        System.out.print("Var is: " + One.getTest());

    }


}

Thanks,

Naz

© Stack Overflow or respective owner

Related posts about java

Related posts about class