constructor | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

constructor

classDemo2 { inta,b; Demo2(){ System.out.println("default constructor"); a=5;b=10; System.out.println("value of a = "+a); System.out.println("value of b = "+b); } classDemo1{ public static void main(String args[]) {Demo2 d2=newDemo2(); } }

8th Aug 2016, 7:17 AM
€¥~RTH~€¥
€¥~RTH~€¥ - avatar
2 Answers
+ 1
the Constructor has to be the same name as the class. So class Demo2 constructor has to be called Demo2()
8th Aug 2016, 7:56 AM
James
James - avatar
0
a constructor is called at the instance when an object is created. there is semantic error in your program. that is class and Demo2 should have space (class_class name). And the closing parentheses of class Demo2..
15th Aug 2016, 3:53 AM
Gowtham krvz kv
Gowtham krvz kv - avatar