+ 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(); } }
3 Answers
+ 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;
}
+ 1
You can simply write-
Testing123(){
System.out.print("hi");
}