Hello;

I have a following method that allocates memory for some objects.

public static int formStyleOfLyle(int a)
	{
		ObjectL[] numberL = new ObjectL[a];

		if(numberL==null)
		{
			return -1;
			}

		return 0;
	}

I really dont know what this does; but I need to check the return value of this method in my main method for problems in memory allocation. Is this even possible, am I thinking alright or am I once again on the road to nowhere :).
Thank you for any info.

Cheers, Joey

EDIT : I know how to chek the return value and everything; but I dont know if the control statement up there does what I need it to do..

That doesn't allocate any memory for ObjectL objects - it allocates memory for an array of int a references (pointers) to possible ObjectL objects.
To allocate memory for the objects themselves you need to execute new ObjectL(...); where ... is zero or more parameters as required by ObjectL's constructor(s).

Because you defined and initialised the array numberL on line 3, it can never be null on line 5, so the test is redundant.
Note also that the array numberL was defined inside the formStyleOfLyle method, so it will go out of scope and be garbage collected as soon as you exit that method.

In summary, that whole method is equivalent to

public static int formStyleOfLyle(int a) {
   return 0;
}

What exactly do you want to achieve here?

That doesn't allocate any memory for ObjectL objects - it allocates memory for an array of int a references (pointers) to possible ObjectL objects.
To allocate memory for the objects themselves you need to execute new ObjectL(...); where ... is zero or more parameters as required by ObjectL's constructor(s).

Because you defined and initialised the array numberL on line 3, it can never be null on line 5, so the test is redundant.
Note also that the array numberL was defined inside the formStyleOfLyle method, so it will go out of scope and be garbage collected as soon as you exit that method.

In summary, that whole method is equivalent to

public static int formStyleOfLyle(int a) {
   return 0;
}

What exactly do you want to achieve here?

Okay, so because I never instantiated an object of type ObjectL there is nothing to return right?
Well I was trying to see how I could get a return value from a method if memory allocation fails [Im a newbie]. So basically I cannot get that unless I actually construct an object in that array of pointers?

Yes, you need to create new Objects, one for each array element, not just references (pointers) to objects.
The only way memory allocation is going to fail is if you run out of memory in the Java VM. In that case you will get a OutOfMemoryError exception thrown, but frankly there's not a lot you can do practically at that point except let the program crash and increase the VM memory settings for the next run.

Yes, you need to create new Objects, one for each array element, not just references (pointers) to objects.
The only way memory allocation is going to fail is if you run out of memory in the Java VM. In that case you will get a OutOfMemoryError exception thrown, but frankly there's not a lot you can do practically at that point except let the program crash and increase the VM memory settings for the next run.

Thank you very much for your help sir james.
Back to the old drawing board for me then :)
Cheers, Joey

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.