• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructor cannot call instance method ?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Please let me know why I am getting the compiler error as given in the comments.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On which object you are invoking that instance method?
 
Greenhorn
Posts: 17
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the following:
Cannot_refer_to_an_instance_method_while_explicitly_invoking_a_constructor
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
During the execution of the constructor- The object hasn't yet been created completely. So the access to instance methods is not possible. You may try static methods.
 
Raja Narasimha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Abimaran & Trishul. I got the point theoretically. I would appreciate if you can provide the reason behind that.
 
Raja Narasimha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mohamed.

mohamed sanaullah wrote:During the execution of the constructor- The object hasn't yet been created completely. So the access to instance methods is not possible. You may try static methods.

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raja Narasimha wrote:Thanks Abimaran & Trishul. I got the point theoretically. I would appreciate if you can provide the reason behind that.


After completion of an instance only you can invoke any instance method on that. That's the problem here.
 
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let me explain that how things happen when you create an object... This i have read from sun's official java book so i would like to share it with you

when you say myclass c= new myclass();

series of actions being performed

1) Bind constructor parameters
2) if explicit this() , call recursively and then skip to step 5
3) call recursively the implicit or explicit super(..) except for Object because Object doesn't have any super class
4) Execute the explicit instance variable initializers. ( e.g you provide for instance variables like this int i=110; in class body )
5) execute the body of the current constructor

now you think before call is made to this() and super() there is no existence of instance variables so how can you call them before call is made to super() or this()(if any )...
that's the reason for error....

i hope your doubt is clear.....
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic