0
Can the instance variables of one class be used in another class?
Is there anyway to do this depending on different access specifiers or some other way perhaps? Please help me.
4 odpowiedzi
+ 2
Its depends on access specifiers ..
Public variable : Able to access from anywhere in the program.
Private variables: only accessible within the declared class..
For others refer this link:
Im'ma Coder R
https://www.javatpoint.com/access-modifiers
..
https://java-answers.blogspot.com/2012/01/access-specifiers-in-java.html?m=1
Hope it helps..
+ 1
Jayakrishna🇮🇳 the first link is 404
0
Im'ma Coder R now check. (A dot accidentally added at end , but now removed it)
0
or you can provide a reference
public class A {
private int[] arr2 = {5,6,7,8};
int[] getArr() { return arr2; }
}
public class Program {
public static void main(String[] args) {
var objA = new A();
int[] arr1 = objA.getArr();
System.out.println( arr1[0] ); // 5
}
}