I study a method call in a method (I do not know the exact term).

I wrote a mini-test (work) program in which I set some data to the object and output it. The program is working, but is it written correctly or can it be made better?

Interested in calling a method of one class through a method of another class. So, for example, I call the method of the class CarImp.java - getCountry using country.getCar() .

PS The question asked earlier on this topic in order to understand what I want to achieve - link to Stack

MainTest.java

 public class MainTest{ public static void main(String[] args) { Country country = new CountryImpl(); country.getCar().setCountry("German"); System.out.println(country.getCar().getCountry()); System.out.println(country.getCar().getEngine()); } } 


Country.java

 public interface Country { Car getCar(); } 

CountryImpl.java

 public class CountryImpl implements Country{ public final Car car; public CountryImpl() { this.car = new CarImpl(); } @Override public Car getCar() { return car; } } 


Car.java

 public interface Car { String getEngine(); void setCountry(String country); String getCountry(); } 

CarImpl.java

 public class CarImpl implements Car { String country; String brand; @Override public String getEngine() { if(country.equals("Germany")) return "Good engine"; if(country.equals("Japan")) return "Many times better than German"; return "Bad engine"; } public void setCountry(String country) { this.country = country; } @Override public String getCountry() { return country; } } 
  • And what should be connected with? Why do you have a car - a property of the country, and not vice versa? - Zufir
  • The country can produce only one brand of car? - Igor
  • @Zufir it does not matter. Only country.getCar().getCountry() , country.getCar().setCountry("German") Why is the country first? And what if I write a house method? That will then be, for example, contry.getHouses.get(0).getHouseName - Antonio112009
  • @Igor This is an example. No more. - Antonio112009

1 answer 1

"calling a method of one class through a method of another class"

We will articulate more clearly. What does this phrase mean?

You want to say - "call the method of the object returned by the previous method"?

There is no fundamental difference between

 country.getCar().setCountry("Germany"); 

and

 Car car = country.getCar(); car.setCountry("Germany"); 

not. In my opinion, the first approach encourages writing long, unreadable call chains. I can justify this (with a stretch) when all methods return the same thing — the object whose methods are invoked. Then the reader of the code does not need to figure out at every step what has returned and whose method is now being called.

  • It is a bit difficult for me to formulate thoughts in Russian. I know it. It became interesting to me to learn and understand how Java libraries are created in order to receive such chains. Now I am writing a bot for Discord and use the JDA library. And there it is very convenient to get any values ​​you need. - Antonio112009
  • For example: MessageReceivedEvent event = new MessageReceivedEvent(); int id = event.getGuild().getMembers().get(0).getUser().getId(); MessageReceivedEvent event = new MessageReceivedEvent(); int id = event.getGuild().getMembers().get(0).getUser().getId(); - Antonio112009