You can pass an instance of bat
into nah
, then reference a
:
public class bat
{
public int a;
void valueA()
{
int a = 20;
}
}
class nah
{
public int b;
void valueB(bat someBat)
{
b = someBat.a;
}
}
Or make a
a static variable, so that you don't need an instance of bat
to reference it:
public class bat
{
public static int a;
void valueA()
{
int a = 20;
}
}
class nah
{
public int b;
void valueB(bat someBat)
{
b = someBat.a;
}
}
Regarding that void valueA()
method in bat
...
In the first case, calling bat.valueA()
is not possible from outside the class because it's not public
. Also you're assigning 20
to a local variable that goes out of scope when valueA()
ends.
In the second case, you couldn't call valueA()
without making it public and static... and you'd still be assigning to a local variable, not the class-level variable.