aspose file tools*
The moose likes Beginning Java and the fly likes Constructor Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor Question" Watch "Constructor Question" New topic
Author

Constructor Question

Herbert Morriss
Greenhorn

Joined: May 21, 2005
Posts: 16
I want multiple constructors. I am trying to get one constructor to call another but keep getting an error. Why cann't I do this?


public class Constructors
{
int x ;
String y ;

public Constructors( int x, String y)
{
this.x = x ;
this.y = y ;
}

public Constructors( int x )
{
String z = Integer.toString( x ) ;
Constructors( x , z ) ;
}
}


E:\Java\Sandbox\Constructors.java:18: cannot find symbol
symbol : method Constructors(int,java.lang.String)
location: class Constructors
Constructors( x , z ) ;
^
1 error

Tool completed with exit code 1
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Originally posted by Herbert Morriss:
I want multiple constructors. I am trying to get one constructor to call another but keep getting an error. Why cann't I do this?


Two issues... First to call another constructor, you used this(). Second, a contructor can only call another constructor as the first call. So, in this case, you constructor should look like this...



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Herbert Morriss
Greenhorn

Joined: May 21, 2005
Posts: 16
Thanks Henry.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor Question