How to call one constructor from another in Java? (this() and super())
Hello everyone! I'm a beginner Java developer, and I have a problem with constructors. I have a class `Car` with several fields: `brand`, `model`, `year`, and `color`. I want to create several constructors: one with a full set of parameters, a second with only brand and model parameters (year and color default), and a third with no parameters at all (all fields default). \n\nI tried to write it like this:\n\n```java\npublic class Car {\n String brand;\n String model;\n int year;\n String color;\n\n public Car(String brand, String model, int year, String color) {\n this.brand = brand;\n this.model = model;\n this.year = year;\n this.color = color;\n }\n\n public Car(String brand, String model) {\n // Here I want to call the first co
No answers yet.
Log in to leave an answer
Blogs
How to Write a REST API in C++ in 30 Minutes: A Quick Start
Generics in TypeScript: A Complete Guide to Usage
Working with Dates and Time in Rust: Advanced Techniques and Libraries
Java for Machine Learning: Capabilities and Limitations in Practice