How can I make two instances of a class, update the same array from that class? Like here, I want r1 and r2 to update same pendingOrders, so that finally the array is [‘yo’, ‘lo’]. That is not happening, the r1 and r2 are making different arrays. Answer Add the static keyword to the pendingOrders in Robot. Check this guide
Tag: oop
Correct Use Of Factory Pattern?
I have been doing research on the factory design pattern and was wondering if the below example, while not “textbook” is technically a correct use of it. Is there another design pattern that may fit the situation better? Answer IMO: Looks like the given code example actually implemented the Factory Method pattern with using the Method Object solution https://refactoring.guru/replace-method-with-method-object. Because
Update constructor using method
I have a game app and I want it to update the constructor strength value when I call the levelUp() method. But when i do System.out.println(hero.hp); after calling levelUp() it returns the first value. Answer One way to always get an up-to-date value for a calculation that depends on other variables/inputs is to calculate it on demand (useful for simple
Two classes and one method with different implementation for each of them
I’m just starting to learn OOP in Java, so this might be a dumb question. The Cat class extends Animal, they have a constructor, a static array, a method that fills the array, and a method that creates an object of the Moving class. On this created object of class Moving, the walk() method can be invoked. Question: how to
How can I override object methods inside interface?
All classes in java extend the Object class implicitly. But that doesn’t concern interfaces. Interfaces can only extend other interfaces, but no classes. However, I can override object class methods inside my interface. Can you please explain in simple words how is this even possible? Is there any use case for this? Answer Interface is a just contract. It says
Expose `final static Set` of a class
I have a class that has: public static final Set<String> IDS = new HashSet<>();, whose values are initiated in a static block after running some query (Therefore I can’t declare it as unmodifiableSet). Now that other classes need to use IDS but apparently I don’t want to let them get direct access to it to avoid IDS being changed by
What is the meaning of breaking code while implementing new features?
I was reading a text about SOLID principles and Open/Close principle and it said that : “The main idea of this principle is to keep existing code from breaking when you implement new features” what it means by breaking code? Answer You “break” existing code when you modify it in a way that prevents it from working for existing or
How to add items to array of objects using loops
I’ve got a multiple student objects I want to write into with a CSV file containing their details. I’ve set each row of the CSV file to an array then was going to split each entry of the array into another array and use that to set the attributes of the object. However, each time I try, I get a
Runnable interface instantiation
I am following the book Head first java and was reading about threads and came to know about the Runnable interface. Now we know an interface can’t be instantiated directly. Its members are implemented by any class that implements the interface , but below code made bouncers over my head. The book declares the instance of the Runnable interface and
Sorting an arraylist with lambda function isn’t working
I’m trying to use .sort() method with an arraylist. My method public void sortSurname() should sort alphabetically all the objects by their surname, these objects are contained in an arraylist ArrayList<Contact> contacts. Could someone please find the issue with this code? Here is what I’ve written so far: Contact class Rubrica class Main Thank you for the help. Answer The