Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » constructor
constructor [message #195873] Wed, 16 February 2005 17:25 Go to next message
Eclipse UserFriend
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 Go to previous messageGo to next message
Eclipse UserFriend
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 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: constructor [message #195908 is a reply to message #195892] Thu, 17 February 2005 03:11 Go to previous message
Eclipse UserFriend
Originally posted by: user.server.com

Independent of this, you cannot call a constructor of an abstract class.

Tom
Previous Topic:Outline View: Showing Inherited Methods
Next Topic:VIM
Goto Forum:
  


Current Time: Thu Nov 06 14:43:25 EST 2025

Powered by FUDForum. Page generated in 0.31552 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top