-1

I want to initialize objects of MyClass using "=" sign.

Ex. String a = "hello";
I want to do
MyClass a = 30;

here is the Class I have written using constructor

public class Duration {
    private long seconds = 0;

    public Duration(int seconds) {
        this.seconds = seconds;
    }
}
msiddique
  • 37
  • 7

1 Answers1

2

This is not possible. Such initialization is only possible for special classes - String and the wrappers of numeric primitive types (Integer, Double, ...).

Eran
  • 387,369
  • 54
  • 702
  • 768