• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why cannot we access the first element or any element of an anonymous array  RSS feed

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any specific reason y we cannot access the first or any element of an anonymous array?




 
Jhonson Fernando
MyExamCloud Software Support
Ranch Hand
Posts: 692
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can access elements of anonymous array.

 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:is there any specific reason y we cannot access the first or any element of an anonymous array?



What is it about that code that is causing you issues?
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Dave,

The code as such is not causing any issues. I am unable to understand from the anon array aspect as in In the main method after we create an anonymous array, i am unable to find a way to access the first element of the anonymous array.
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Johnson,

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.



 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


This is true for any object (or any type of data). If you don't have a variable in scope, you don't have access to that data, from that context.

Henry
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Henry

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..
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not limited to arrays:

After that statement, you cannot access the Something instance from the calling routine because you have no reference to it.
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.]
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • In Java array is reference type so you create an object of it like class, String.
  • In java we can create an anonymous object of reference types like array, class, String etc.

  • 1. Array object with reference variable, referencing to it:
  • Below code has reference to an array of int type named myNumberArray;
  • Whenever you want to access or operate on the components (We can say data elements)of this object of array of type int, then you have reference variable myNumberArray which referes to an object of that array containing values 10,20,30,40. You can use that reference any number of time to operate on that array.
  • Example:
  • Output:
    Element at oth index: 10
    Element at 1st index: 20

    2. Array object without reference variable also called Anonymous array:
  • You can create an array without storing it's reference to reference variable that array is called anonymous array.
  • 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. 
  • Below code creates two anonymous arrays of int type having elements 10,20,30,40 and 50,60,70,80 respectively.
  • Output:
    Anonymous array object1 element at 1st index: 20
    Anonymous array object2 element at 0th index: 50

  • You can operate on it only once so you can use any method of that array. Lets see another example
  • Output:
    Length of anonymous array object1 is: 4
    Length of anonymous array object2 is: 3

    Real life example:
  • Any object must have name like chair, table, fan, TV etc so when I say bring chair for me to sit no one will bring fan.
  • Use of that reference variable is pointing to some unique object.
  • Hope you understood
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Ranch Hand
    Posts: 1143
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.


    Assigning it to a variable isn't the only way to keep a reference to an anonymous array "live".  You could chain together any number of methods that each accept an array of ints (or whatever base type) and return that same array.  Then you could chain any number of calls together:



    The anonymous array will be created, and then the static methods will be called.  Only after the outer call to printInOneLine() returns is the array eligible for GC -- no variable assignments needed.

     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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. 

    I already mentioned it is eligible for GC if it's reference is not used by assigning it to reference variables.
    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.
    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
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    An object is eligible for GC when there not anymore reference to it. So there is only way to keep reference alive by assigning the reference of that object to reference variable. Then that reference variable can be method parameter, array component, Constructor parameters, an instance variable, a class variable, Local variables. In anonymous object of array we have reference, if we don't assign it to reference variable of that array type then we can use it only once. You can assign it by passing reference as parameter to method, constructor etc. Please correct me If I'm wrong
     
    Ryan McGuire
    Ranch Hand
    Posts: 1143
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.


    Point taken.  I think we both understand exactly how things work and are just having a disagreement over semantics.  Yes, there is an implicit variable assignment when the argument from a method call is assigned to the corresponding parameter.  I was pointing out (perhaps unsuccessfully) that this isn't an explicit variable assignment using an assignment operator.
     
    Piet Souris
    Master Rancher
    Posts: 2044
    75
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ehhh...       
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Ryan McGuire yes got your point
     
    krishnadhar Mellacheruvu
    Ranch Hand
    Posts: 118
    Android Java Objective C
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.]


    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.

    This would do if am not wrong



    The output is:

    10
    20
    30
    40

    The value of first index value is:10
     
    krishnadhar Mellacheruvu
    Ranch Hand
    Posts: 118
    Android Java Objective C
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
  •  


    Hi

    Ganish,

    That's clearly making scence to me
     
    krishnadhar Mellacheruvu
    Ranch Hand
    Posts: 118
    Android Java Objective C
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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...
     
    krishnadhar Mellacheruvu
    Ranch Hand
    Posts: 118
    Android Java Objective C
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi


    Ryan/Ganish

    The last piece of code line in main method



    is countering my initial question of this post. So there is a way to access the values of an anonymous array...
     
    Campbell Ritchie
    Marshal
    Posts: 56593
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    krishnadhar Mellacheruvu wrote:. . . So there is a way to access the values of an anonymous array...
    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.

    You can do things like adding the array to a List:-
     
    krishnadhar Mellacheruvu
    Ranch Hand
    Posts: 118
    Android Java Objective C
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi

    Campbell,

    That is what my initial impression was but when i have this piece of line



    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..
     
    Campbell Ritchie
    Marshal
    Posts: 56593
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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..
    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.
    If you need to use the same array again, it needs a reference in scope.

    Remember that arrays are always mutable, so if you want arrays always having the same content you would have to do something like this:-Of course, it is much easier (and probably better) to stick to a List<Integer> and not bother with arrays at all.
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    krishnadhar Mellacheruvu wrote:
    That is what my initial impression was but when i have this piece of line


    I think one cause of the confusion here may be trying to establish a single rule for something that is more complex than that.

    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).

    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..


    I guess, given the example, you argue that it is correct... but given the complexity, you can also show that it is wrong, simply with a different example.

    Henry
     
    krishnadhar Mellacheruvu
    Ranch Hand
    Posts: 118
    Android Java Objective C
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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).



    Thanks Henry for the insightful info
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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...

  • It is the job of daemon thread to reclaim the memory of unreferenced object.
  • After being eligible for GC, after how much time it's memory will be reclaimed we can't guarantee that.
  • Different Java run-time implementations will take varying approaches to garbage collection.
  • So GC relies on thread scheduling which is not completely in control. This point can be added as disadvantage of Java.
  • BUT also remember advantage that Java furnishes automatic garbage collection mechanism where we don't have to deallocate memory manually like other language like C where memory leakage etc. etc. occurs.
  • Hope you got that
     
    Ryan McGuire
    Ranch Hand
    Posts: 1143
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.


    I don't understand what you're saying.  The large chunk of code I provided works as I would expect.  The output looks like...

    Element at 0 10
    Element at 1 20
    Element at 2 30
    Element at 3 40
    100
    The first score is 10

    Even though the total of the array elements is calculated and printed to System.out, the original array is returned from the anon() method with all the array elements intact.  That's what makes it possible to use in the println() in main(). 

    Is that not what you expected?
     
    krishnadhar Mellacheruvu
    Ranch Hand
    Posts: 118
    Android Java Objective C
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi

    Ryan,

    Thanks for taking time to reply.

    anon method is only to add elements of the anonymous array that we declare in main method. my intention was how to the access elements of an anonymous array after the first usage at-least a remote possibility which i assume is not possible after the first usage. And yes your code works absolutely fine.

     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!