Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor Question  RSS feed

 
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
 
author
Sheriff
Posts: 23507
138
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.
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!