Can the instance variables of one class be used in another class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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.

28th Oct 2021, 12:13 PM
ltsRoy
ltsRoy - avatar
4 Answers
+ 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..
28th Oct 2021, 2:39 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 the first link is 404
28th Oct 2021, 5:10 PM
ltsRoy
ltsRoy - avatar
0
Im'ma Coder R now check. (A dot accidentally added at end , but now removed it)
28th Oct 2021, 5:25 PM
Jayakrishna 🇮🇳
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 } }
28th Oct 2021, 10:52 PM
zemiak