Results 1 to 6 of 6
  1. #1
    Roobiks is offline Member
    Join Date
    Mar 2016
    Posts
    19
    Rep Power
    0

    Question Make array global

    Hello there

    I have the following array in the main method:

    Method m[]=c.getDeclaredMethods();

    How do I make it global in order to use the array m[] in a different method?

    Thanks!
    Roobiks

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Make array global

    Any value declared in a method is local to the method. To make it available to other methods, declare it
    as an instance field (right after the class declaration).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Roobiks is offline Member
    Join Date
    Mar 2016
    Posts
    19
    Rep Power
    0

    Default Re: Make array global

    Thank you.
    And how would I do that?

  4. #4
    Roobiks is offline Member
    Join Date
    Mar 2016
    Posts
    19
    Rep Power
    0

    Default Re: Make array global

    Thank you.
    And how would I do that?

  5. #5
    Roobiks is offline Member
    Join Date
    Mar 2016
    Posts
    19
    Rep Power
    0

    Question Re: Make array global

    This is some extract from the main:

    Class<?> c = Class.forName("Igenerator."+s);

    ...

    Method m[]=c.getDeclaredMethods();
    for(int i=0;i<m.length;i++){
    System.out.println(m[i]);}

    ...

    print()

    End of class

    Then there is a method called print()
    There I want to do the following: System.outprint(m[0]);
    Or in general I want to get access to m[] in the method print.

    Can anyone help?

  6. #6
    Roobiks is offline Member
    Join Date
    Mar 2016
    Posts
    19
    Rep Power
    0

    Default Re: Make array global

    Allright I solved the problem. It is just the same as in the main method. There are many methods in the dummyClass:

    public static String print(int j){
    dummyClass cls = new dummyClass();
    Class c = cls.getClass();
    Method[] m = c.getDeclaredMethods();
    System.out.print(m[1]);
    return m[j].toString();

    }

    I created a corresponding Unit Test which calls print with assertequals.
    Unortunately the m[] order is different as in the Unit Test. How do I sort it?

    If I use Arrays.sort(m); this will be printed out:
    Exception in thread "main" java.lang.ClassCastException: java.lang.reflect.Method cannot be cast to java.lang.Comparable
    Last edited by Roobiks; 04-03-2016 at 06:19 PM.

Similar Threads

  1. How can I make an array of JButtons?
    By LuxoJr in forum New To Java
    Replies: 2
    Last Post: 02-27-2012, 03:51 AM
  2. How do you make an array of arrays?
    By mrjaeyun in forum New To Java
    Replies: 1
    Last Post: 11-14-2011, 12:34 AM
  3. Got problem on accessing public (global) array
    By smtwtfs in forum New To Java
    Replies: 6
    Last Post: 02-15-2011, 09:02 AM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. want to make an array
    By doha786 in forum New To Java
    Replies: 3
    Last Post: 03-29-2009, 01:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •