I have the following code:
public static void main(String[] args) {
try {
String[] studentnames = {
/* this is an array of 9000 strin
Just externalize Strings from your code to one *.properties / xml / json file. Netbeans and/or Eclipse can do it for you easly.
Initialising the studentnames array is counting towards the size of the main method. As there are 9000 student names each name can only be about 7 characters before you'll run out of space. As the others have stated you need to reduce the size of method. You can split it into pieces as Pramod said but in this case the bulk of the method is actually data. I would do as Infiltrator says and split the names out into a separate file and just read it in your main. Something like commons-io can be used to get you to effectively the same position you're starting in.
List<String> namelist = FileUtils.readLines(new File("studentnames.txt"));
String[] studentnames = namelist.toArray(new String[0]);
You may find it useful to process the list rather than convert it to an array or alternatively you could use a LineIterator instead
LineIterator it = FileUtils.lineIterator(file);
try {
while (it.hasNext()) {
String line = it.nextLine();
// do something with line
}
} finally {
it.close();
}
In java a methods can't have more than 65535 bytes.
So to fix this problem, break up your main(String[] args)
method in to multiple sub methods.
Solution number 1:
public static void main(String[] args) {
int[] num ={100001,100002,.......,...};
}
Suppose the array num
contains 100000 numbers, and your main
function gives an error. In such scenario, you can split the above array in two parts. Now, you can declare 2 different arrays instead of just a single array. For example:
static int[] num1 ={}
and static int[] num2={}
Declare num1
outside of the main function and num2 inside the main function where num2
will replace num
inside the main function.
All elements of the array num
will be split into two subarrays num1
and num2
; split in such a way that both a static and non static array could hold say 8000 and 2000 elements.
The code inside the main method can handle all 10000 elements, an assumption only:
if (n <= 8000) {
System.out.println(" ---> " + num2[((int) n - 1)]);
} else if (n > 8000 && n <= 10000) {
n = n - 8000;
System.out.println(" ---> " + num1[((int) n - 1)]);
}
So splitting that array or say dividing the elements in two parts of 8000 and 2000, could remove the error.
Solution number 2:
Put the array outside main
method, both num1
and num2
are now static arrays but the 65535 bytes limit holds also for static initialised arrays. Split your elements, for example, you can split 10000 in 8000 and 2000, or as per you requirement in two static arrays outside the main method.
This error is related to the storage of elements in long[]
or int[]
where the error occurs if the storage size surpasses the limit of 65535. In such a scenario go splitting the elements, for example if you are using long[]
or int[]
split them in suitable numbers till the error gets resolved.
In other words, it is not mandatory to only split the array into 2 parts, you can divide them in 4 different sizes also or different equal parts. But again the solution to your problem may vary as per your requirement.
The error code seems quite self-explanatory.
The code of method main(String[]) is exceeding the 65535 bytes limit
This is because there is an arbitrary hard-coded limit in Java of 64Kb for method sizes. (And actually many other things are limited to 64K, such as method names, the number of constants, etc. See the Java 8 specs or the Java 7 specs for more details.)
To work around this, all you have to do is break up your main(String[] args)
method into multiple sub-methods.
But why not just load the names from a file instead?
Some of the issues with doing it the way you are currently proposing are:
firstly, you're hard-coding details, which is almost always a bad thing (See this);
secondly, you're getting that error message; and
thirdly, you make your code very difficult to read.
There are many more, of course, but these are the obvious ones.
I would read the students names from a file however one work around which will make the class smaller as well
String[] studentnames= "Student names is an array of :9000".split(" ");
Instead of defining and using 9000 strings, this uses just one. The limit for a single String is just over 2 billion.