I have an issue where I need an array as a global variable. If someone could tell me if that is possible and/or how to do that then that would be great. (insert meme here XD).
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.
I have an issue where I need an array as a global variable. If someone could tell me if that is possible and/or how to do that then that would be great. (insert meme here XD).
There is no such thing as a global variable in Java.
The closest parallel is to create a public static member of a class, then every class which wants to use that "global" would reference it from the given class.
public class MyGlobals { public static int[] data = new int[]{1,2,3}; }
An alternative is to employ the singleton idiom.
lil_misfitss (August 15th, 2013)
If you explain WHAT you're trying to do - a code example would be great - someone here can explain HOW to do it. The solution won't include the approach you've suggested.
you make a global type array just as like you instantiate a array. But one thing remind it, that array elements are always get a default value irrespective of the position of a array.
lil_misfitss (August 15th, 2013)
Thank you all for the help! Especially Rahul Bansal and helloworld922. My professor called the example helloworld922 gave, global variables. Sorry for the misuse of the term.