Yes, this is perfectly normal in Java
Passing a New Object as an Argument: Is it Possible?
-
27-06-2023 - |
Question
Is it possible to do the following - allowing for a properly coded constructor:
object1.method(new objectType1(argument1, argument2));
Here, object1 contains objects of objectType2.
So, consider the following code:
portfolio.addInvestment(new StockInvestment(stock, numshares));
addInvestment adds an object of type StockInvestment to an ArrayList . So, addInvestment receives an object of type StockInvestment
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow