Results 1 to 5 of 5
- 06-15-2011, 08:58 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 10
- Rep Power
- 0
Function call returning array of objects
Hi all,
.gif)
I want to know how to call a function having same return type as that of class which returns array of objects. Problem i'm facing is how to call the function and catch the array.
Here is the code i'm trying to execute:
public class A{
private int x;
private int y;
public A(int j, int k)
{
x=j;
y=k;
}
public A create()
{
A[] obj= new A[5];
for(int n=0;n<5;n++)
{
obj[n]=new A(n,n+1);
}
return obj;
}
public static void main(String args[])
{
A[] ob = new A[5];
ob= create();
System.out.println(ob[0].x+" "+ob[0].y);
}
}
Regards,
ShitalJain.
- 06-15-2011, 09:11 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Not sure what "A" is but
Change
TOJava Code:public A create()
Java Code:public A[] create()
- 06-15-2011, 09:15 AM #3
And Java has methods, not functions.
db
- 06-16-2011, 01:19 AM #4
Why waste resources creating an array on line 1 when you immediately throw it away on line 2? Also since create is an uinstance method you cannot access it from main without an instance of the A class. Therefore your code should be:Java Code:A[] ob = new A[5]; ob= create();
Java Code:A a = new A(); A[] ob = a.create(); // or A[] ob = new A().create();
- 06-25-2011, 09:39 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Call a function via URL
By cali_guy_0024 in forum New To JavaReplies: 7Last Post: 04-10-2011, 05:04 AM -
Polygons... returning and passing objects?
By asherwolf in forum New To JavaReplies: 3Last Post: 07-09-2010, 04:41 PM -
function call error
By peter_thm in forum New To JavaReplies: 2Last Post: 01-13-2010, 12:57 PM -
Rewrite as a function so it can call from main.
By thangli in forum New To JavaReplies: 2Last Post: 11-30-2008, 06:26 AM -
help with System.exit (1) function call
By katie in forum Advanced JavaReplies: 2Last Post: 08-06-2007, 08:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks