• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question regarding constructor working  RSS feed

 
Khuzema Dharwala
Ranch Hand
Posts: 61
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Folks,

Can someone please explain me why this program is not compiling.




The error on compilation is as follows

>javac Hello.java
Hello.java:8: cannot find symbol
symbol : method Hello()
location: class Hello
Hello();
^
1 error


Thanks...
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors are not methods - you cannot call them using their name. If you want to call the no-arg constructor from one of your other constructors, use
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say Hello(); it is as good as invoking a method doSomeStuff();
Change Hello(); to this();
 
Khuzema Dharwala
Ranch Hand
Posts: 61
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Maneesh and Stuart it clears my doubt, I know that constructor aren't method and only a constructor can call a constructor but I wasn't aware of the fact that the calling can be done using "this" only.

Much appreciated.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!