I'm trying to understand the initialization of the object property directly but getting a compilation error.
In C#, the below code works:
new Employee() {
Name = "John"
}
In Java, can one please tell me the equivalent of the same, as the below piece of code throws compilation error:
new Employee() {
Name = "John"
} // Throws error in Java
public class Employee {
public string Name;
}
I don't want to use the constructor to initialize the object, just for my understanding of the Java syntax. Please let me know if there is a way to initialize the object in Java as in C# like:
new Employee() {
Name = "John"
}