« first day (4429 days earlier)      last day (515 days later) » 

12:00 AM
grep me no patterns and I'll tell you no lines. (source)
 
 
9 hours later…
8:53 AM
uses java.io.File
 
9:07 AM
because you might want to do
public class Car {
    String color;
    public Car(int color) {
        this.color = Integer.toString(color);
    }
}
but generally speaking, you wouldnt
and even if you do want to, it is recommended to make a static factory method
however, the easy solution is just to use records
because making a constructor with all the parameters and getters and some default methods is so common and recommended, Java adds all of that boilerplate under a single, non-redundant definition
public record Car(String color) {} would add all of it for you
 
 
6 hours later…
3:36 PM
@Wietlol I don't understand "what I might want to do"
 
what if you want a constructor with a different type parameter than the field type?
 
4:02 PM
@Wietlol why can't I introduce it as an int variable right away (if I want it to be an int variable)?
 
you could
 
@Wietlol then what's it all about?
Or you could even write something like this, it's even simpler

public class Car {

public Car(String color) {
}

public static void main(String[] args) {

Car ferrari = new Car(“red”);
}
}
 
you could write something like that, but then you just discard that parameter as you are not using it
again, if you want the simple approach, use records
if you want more control, and non-standard classes, use class
 
4:21 PM
@Wietlol why am I not using the parameter? I wrote public Car(String color) and then Car ferrari = new Car(“red”)
 
yes, but you dont use that String color
public Car(String color) {
    // if this body is not using the "color" parameter, it is unused and might just as well be removed
}
 
I know it's not how Java works, I ask why it works the way it does (and not the way I suggest). There must be some rationale behind it
 
 
1 hour later…
5:26 PM
primarily because control
and explicitness
but with records, it is how Java works
 
6:20 PM
But when creating a non-constructor method, I introduce a parameter without creating and specifying a separate instance field. So we have, for example

public class Store {
// instance fields
String productType;
double price;

// constructor method
public Store(String product, double initialPrice) {
productType = product;
price = initialPrice;
}

// increase price method
public void increasePrice(double priceToAdd){
double newPrice = price + priceToAdd;
price = newPrice;

}

instead of

public class Store {
 
 
1 hour later…
7:37 PM
Hey,

I have a HashMap like the following,

Map<Car, List<Parts>> carParts = new HashMap<>();

I have already implemented the equals and hashcode for the Car class. I am using SpringBoot and when I am trying to make a GET request to get this carParts and when I do that the JSON body is something like this,

{
"Car [name=Sedan, id=1, hashCode()=128711123]": []
}

Why is this map key calling toString() method and not converting as JSON?
 
@AbrahamArnold Hi using SpringBoot, I'm Oak!
@AbrahamArnold Welcome to the Java Chat, the room for Java enthusiasts! I'm Oak, one of the room's bots. If you want to ask a question, just ask it and someone will respond if they feel like it. But remember that this room is not a help desk or tutoring service! If you want to just hang out, then welcome aboard! Oh, and the room's full list of rules are posted here.
 

« first day (4429 days earlier)      last day (515 days later) »