Learning to Code
Jhonson Fernando
MyExamCloud | AI Course Generator | 1Z0-829 Practice Tests | Java 8 Mock Exams | 1Z0-811 Practice Tests | 1Z0-900 Practice Tests | PCAP Practice Tests | PCEP Practice Tests | PCPP1 Practice Tests
krishnadhar Mellacheruvu wrote:is there any specific reason y we cannot access the first or any element of an anonymous array?
Learning to Code
Learning to Code
krishnadhar Mellacheruvu wrote:
We can access the elements in the anon method.. but in the main method after we create the anonymous array am unable to find a way to access the elements.
Learning to Code
krishnadhar Mellacheruvu wrote:So since there is no variable created for the anonymous array, we do not have access to the data of anonymous array. Am i correct..
Being Java programmer.
Being Java programmer.
Ganish Patil wrote:Forgot to tell you, An object of anonymous array is eligible for garbage collection immediately after it's first use, if it's reference is not used second time anywhere by assigning it's reference to reference variable of that array type like in your code you assigned reference of array of int type i.e. new int[]{10,20,30,40} to int[] x in non(int[] x) static method.
Ryan McGuire wrote:Assigning it to a variable isn't the only way to keep a reference to an anonymous array "live". no variable assignments needed.
In your code also you are assigning reference of anonymous object of array int[] {10, -20, 40, 30, -10, 20, -30, 25} to method parameter's reference variable(one of the 8 kinds of variables, whose scope is withing that method, Ref: point 4 of JLS 8 4.12.3 ) int[] x of method public static int[] mean(int[] x). Same way you did with rest of static methods.Ganish Patil wrote: if it's reference is not used second time anywhere by assigning it's reference to reference variable of that array type like in your code you assigned reference of array of int type i.e. new int[]{10,20,30,40} to int[] x in non(int[] x) static method.
Being Java programmer.
Being Java programmer.
Ganish Patil wrote:In your code also you are assigning reference of anonymous object of array int[] {10, -20, 40, 30, -10, 20, -30, 25} to method parameter's reference variable(one of the 8 kinds of variables, whose scope is withing that method, Ref: point 4 of JLS 8 4.12.3 ) int[] x of method public static int[] mean(int[] x). Same way you did with rest of static methods.
There are three kinds of actuaries: those who can count, and those who can't.
Being Java programmer.
Ryan McGuire wrote:
krishnadhar Mellacheruvu wrote:So since there is no variable created for the anonymous array, we do not have access to the data of anonymous array. Am i correct..
Exactly. The obvious way to access the members of an anonymous array after its first use is to assign the array to a variable, pass that to the method and then access as needed:
You could also have the anon() method return its array argument.
This second way seems less readable to me though.
[Disclaimer: I haven't actually tested either of my solutions.]
Learning to Code
Ganish Patil wrote:
Keep in mind, you can operate on anonymous array only once during it's creation, unless and until you store it's reference in a reference variable for further use as you did it in your code by passing reference of array to anon(int[] x) method.
Learning to Code
Ganish Patil wrote:Forgot to tell you, An object of anonymous array is eligible for garbage collection immediately after it's first use, if it's reference is not used second time anywhere by assigning it's reference to reference variable of that array type like in your code you assigned reference of array of int type i.e. new int[]{10,20,30,40} to int[] x in non(int[] x) static method.
Learning to Code
Learning to Code
You have already been told that once the variable goes out of scope it cannot be brought back into scope. So the simple answer is no.krishnadhar Mellacheruvu wrote:. . . So there is a way to access the values of an anonymous array...
Learning to Code
No, in that context you cannot use the array again. You can create another array identical to it, but that looks like copy‑and‑paste code to me.krishnadhar Mellacheruvu wrote:. . . i do not have it in scope. If i want to use i need to create a new anonymous array. Correct me if am wrong..
krishnadhar Mellacheruvu wrote:
That is what my initial impression was but when i have this piece of line
krishnadhar Mellacheruvu wrote:
ok wait... here i am using it as and when i am creating the anonymous array hence it is working. If i plan to use it any where else in the code i will not be able to since i do not have it in scope. If i want to use i need to create a new anonymous array. Correct me if am wrong..
Henry Wong wrote:
One. Variables are either in scope or not. And whether they are in scope depends on the location of the code.
Two. Instances (Objects) are reachable or not. And determining whether they are reachable is done by checking all the possible roots, and traversing all the links.
And since it is possible to have many variables reference the same object, it can get complex. Meaning, you can't make the generalization of scope of a specific variable to reachability of a specific object (and hence, can be used).
Learning to Code
krishnadhar Mellacheruvu wrote:
Ganish Patil wrote:Forgot to tell you, An object of anonymous array is eligible for garbage collection immediately after it's first use, if it's reference is not used second time anywhere by assigning it's reference to reference variable of that array type like in your code you assigned reference of array of int type i.e. new int[]{10,20,30,40} to int[] x in non(int[] x) static method.
Can you give an example of this...
Being Java programmer.
krishnadhar Mellacheruvu wrote:Hi ryan
We cannot access the value of the array in main method as given the code since we are using anon method which adds the array elements that we are passing.
Learning to Code
Don't get me started about those stupid light bulbs. |