Question #32061

Hello , if somone can help me with this issue :
If i declare a variable field inside a class , and i need to use that variable in other class , is there a way to do this ?

Expert's answer

Yes, you can.


1st You can declare this field as "public" Example:


class A{
public int i;
}
class B{
public void myMethod(){
int j = A.i;
}
}


As you see you can use field "i" from class "A" using the "A.i".


2nd You can create an accessor method in class A. Example:


> class A{
>
> private int i;
public int getI(){
return this.i;
}
}
> class B{
>
> public void myMethod(){
> A a = new A();
> int j = a.getI();
>
> }
>
> }


As you see you can use field "i" from class "A" using the "a.getI()".

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS