• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

constructor inside another constructor

Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14501
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic