This week's book giveaways are in the Angular and TypeScript and Web Services forums.
We're giving away four copies each of Programming with Types and The Design of Web APIs and have the authors on-line!
See this thread and this one for details.
Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

constructor inside another constructor

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
guys i have a constructor in Date class i want to use this constructor inside another constructor


public Date class
public Date (int day, int month, int year) this is the constructor of Date class

now i have a FoodItem class
and i am trying to make a constructor there that one of the parameters is make a new object in Date class
public class FoodItem
public FoodItem(String name, long catalogueNumber (here must be an new object from Date class)

please help me how to write it right
 
Saloon Keeper
Posts: 10993
243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either give your FoodItem constructor a parameter of type Date, or pass it the values that are necessary to create a Date and then create it in your constructor body. Here are the two alternatives:

The second approach is NOT recommended. Constructors should try to avoid doing any work that can be done by the caller of the constructor, and it also makes their signature harder to read.

On a tangent, if you're on Java 8 or newer, get rid of Date and use LocalDateTime instead.
 
Do you want ants? Because that's how you get ants. And a tiny ads:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!