Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

When do we invoke a method using a dot notation?

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know when do we invke a method using a dot-notation and when do we have to declare a complete method?like saying:
dot notation:
complete method:
public String print(){
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You always invoke an instance method by saying
where you replace "..." by the actual parameters to this method.
If it's a static method, you can say
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you're invoking a method from within the same class that defines that method (or a subclass which inherits the method), then you don't need a dot - you can just call the method. Unless you're in a static method trying to call a nonstatic method - then you need to create an instance and use it to invoke. Or if you're in a non-static method but want to invoke a method using a different instance (not the implicit "this" instance) - then you must name the instance you're using. E.g.

In swapValue() here, getValue() and setValue() operate on the current instance ("this") unless otherInstance is used to invoke. In the main() method (static), it's always necessary to use an a or b reference to invoke a nonstatic method.
[ August 19, 2002: Message edited by: Jim Yingst ]
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic