• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Question regarding constructor working  RSS feed

 
Ranch Hand
Posts: 74
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...
 
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
 
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: 74
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!