Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Gobal array

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Gobal array

    I would like to know if we can dynamically create an array in main method but declare it as global. Is such a thing supported by java?

    Here is my reason,
    I am working with images. So within the main method I create a 2D int array by obtaining dimensions of image and setting size of array during runtime.
    However I would like to write many small functions that operate on this 2D array outside main().
    So I would like to make the array global so it can be accesed by the functions, but declared inside the main.
    Is there any way such a thing is possible?


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Gobal array

    You can declare a variable of any type (including an array) outside any method but initialise it inside a method. That way one method can be responsible for giving the variable a value, but other methods can be passed the variable and, hence have access to that value.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    xpr2 (June 18th, 2013)

  4. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Gobal array

    Quote Originally Posted by pbrockway2 View Post
    You can declare a variable of any type (including an array) outside any method but initialise it inside a method. That way one method can be responsible for giving the variable a value, but other methods can be passed the variable and, hence have access to that value.
    Thanks a lot. I am new to java and hadn't realized we can declare outside if you use the new operator.

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Gobal array

    You're welcome.

Similar Threads

  1. compile errors when creating a 2nd array the same size as 1st array
    By javaiscool in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2013, 09:35 PM
  2. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  3. [SOLVED] Merge two 1D array's into a new 1D array. Trouble appending last index
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2012, 12:57 PM
  4. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  5. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 05:29 PM