I have the following code:
public static void main(String[] args) {
try {
String[] studentnames = {
/* this is an array of 9000 strings... */
};
}
}
I get the following error when trying to compile this:
The code of method main(String[]) is exceeding the 65535 bytes limit
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.
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.
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();
}
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.
The code of method main(String[]) is exceeding the 65535 bytes limit
As your error says the main method exceeds the limit of 65535 bytes.
Reason : Why we got this error in our code?
- We know that the memory allocation for array would be done sequentially. ie; the memory would be allocated for the array if such a huge amount of space is available in RAM continuously.
- We have used String array which holds some "9000" strings - which is too large to hold in the memory
- The RAM might not have such a huge space continuously during that time so, we might have got that error.
Solution : So what can I do for this?
- For storing such huge size value we may prefer file for read/write - so we can read a part of a file when required
- We can store those strings in an ArrayList which grows its memory when element gets added to it instead of allocating memory on a whole initially.
You method is too long.
Create different methods to split it and make it much more readable and maintanable.
Just externalize Strings from your code to one *.properties / xml / json file. Netbeans and/or Eclipse can do it for you easly.