Reputation: 65
how can i get access to method in class from arraylist?
ArrayList car = new ArrayList<>;
car.add(new carClass("Audi", 35000));
System.out.println("The price is:"+ car.get(0) );
i woud like to get only price from this arraylist (method getPrice)
Upvotes: 1
Views: 42
Reputation: 79025
First of all, replace
ArrayList car = new ArrayList<>;
with
List<Car> carList = new ArrayList<>();
and then do as follows:
System.out.println("The price is:"+ carList.get(0).getPrice());
Check this for more understanding on this topic.
Finally, you should follow the Java naming convention e.g. carClass
should be CarClass
or simply, Car
.
Upvotes: 2