Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor Question

 
Herbert Morriss
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Herbert Morriss
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic