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 ?
1
Expert's answer
2013-06-20T11:57:35-0400
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()".
Comments
Leave a comment