| constructor [message #195873] |
Wed, 16 February 2005 17:25  |
Eclipse User |
|
|
|
Originally posted by: user.domain.invalid
i am attempting to creat a generic clone method in a supertype but am
having problems with the constructor not having appropriate arguments.
example:
import java.lang.reflect.Constructor;
public abstract class Demo {
int x;
int y;
int z;
Demo(int x, int y, int z) {
}
public Demo clone(Demo original) {
Constructor constructor = original.getClass().getConstructors()[0];
Demo copy = constructor(1, 2, 3);
return copy;
}
}
wtih an error message:
The method constructor(int, int, int) is undefined for the type Demo
any suggestions?
norwood sisson
|
|
|
| Re: constructor [message #195887 is a reply to message #195873] |
Wed, 16 February 2005 18:08   |
Eclipse User |
|
|
|
Originally posted by: SamMesh.gmail.com
This is Java question, not Eclipse
Please see
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/reflect/Co nstructor.html#newInstance(java.lang.Object[])
and use
> Object[] args = {x,y,z};
> constructor.newInstance(args);
BTW, as we can see you just started to use Java.
"The Java Language Specification" can be a good starting point:
http://java.sun.com/docs/books/jls/
--
10x,
Sam Mesh
user@domain.invalid wrote:
> i am attempting to creat a generic clone method in a supertype but am
> having problems with the constructor not having appropriate arguments.
> example:
>
> import java.lang.reflect.Constructor;
>
> public abstract class Demo {
> int x;
> int y;
> int z;
>
> Demo(int x, int y, int z) {
> }
>
> public Demo clone(Demo original) {
> Constructor constructor = original.getClass().getConstructors()[0];
> Demo copy = constructor(1, 2, 3);
> return copy;
> }
> }
> wtih an error message:
> The method constructor(int, int, int) is undefined for the type Demo
> any suggestions
|
|
|
| Re: constructor [message #195892 is a reply to message #195873] |
Wed, 16 February 2005 18:11   |
Eclipse User |
|
|
|
Originally posted by: richkulp.us.NO_SPAM.ibm.com
The constructor is not a public constructor, you have it as package
protected. This means that getConstructors() won't return it. You need
to use getDeclaredConstructors() to get all constructors for the class.
However, in this case it will be ok, but in general if you tried to
construct from some other class in another package using the declared
constructor it would fail because you would not have the correct access
to it.
user@domain.invalid wrote:
> i am attempting to creat a generic clone method in a supertype but am
> having problems with the constructor not having appropriate arguments.
> example:
>
> import java.lang.reflect.Constructor;
>
> public abstract class Demo {
> int x;
> int y;
> int z;
>
> Demo(int x, int y, int z) {
> }
>
> public Demo clone(Demo original) {
> Constructor constructor = original.getClass().getConstructors()[0];
> Demo copy = constructor(1, 2, 3);
> return copy;
> }
> }
> wtih an error message:
> The method constructor(int, int, int) is undefined for the type Demo
> any suggestions?
>
> norwood sisson
--
Thanks,
Rich Kulp
|
|
|
|
Powered by
FUDForum. Page generated in 0.04200 seconds