Answer to Question #32061 in Java | JSP | JSF for Blla

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 ?
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()".

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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS