1

Dumb question: can I change a constructor in Java to be similar to a String's constructor (for example)? Such as calling

class myclass{}

with

myclass sc =  "text";

Is this possible in any way?

SystemUser
  • 11
  • 3
  • I like the question, but note that in Java, class names should be `CamelCase`, so you might want to change `myclass` to `MyClass`. – domsson May 03 '17 at 11:36
  • 1
    When you assign a string literal to a variable, you're not calling a constructor. So ... no. – khelwood May 03 '17 at 11:36
  • So there is a huge difference between `String a = "text";` and `String a = new String("text");`? – SystemUser May 03 '17 at 11:38
  • 1
    A significant difference, yes. – Kayaman May 03 '17 at 11:38
  • I havent done any java work for a while but does it have the same concept as C# where you can new an object and set properties (in java I guess these would just be fields) on it at the same time `MyClass myClass = new MyClass() { A = "a", B = "b" }` – Max Young May 03 '17 at 11:39
  • @SystemUser There is a difference. If you call `new String` you are constructing a **new** string object from a string literal. – khelwood May 03 '17 at 11:39
  • This might be related: http://stackoverflow.com/questions/32020191/overriding-assignment-operator-in-java – domsson May 03 '17 at 11:41

0 Answers0