This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 23590
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!