+ 1

Constructors in java

How can we use a constructor like in the main method. public class Testing123 { private String hi; Testing123(){ this.setHi("hi"); } } public class Result { public static void main ( String [] args ) { Testing123 TEST = new Testing123(); } }

28th May 2020, 12:05 PM
H-J
H-J - avatar
3 odpowiedzi
+ 2
if you are asking why your code doesnt work, there is not setHi() method Testing123(){ this.setHi("hi"); System.out.println (hi); } void setHi(String hi) { this.hi = hi; }
28th May 2020, 7:13 PM
zemiak
1st Jun 2020, 10:39 AM
Sandra Meyer
Sandra Meyer - avatar
+ 1
You can simply write- Testing123(){ System.out.print("hi"); }
28th May 2020, 12:44 PM
Avinesh
Avinesh - avatar