• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

When do we invoke a method using a dot notation?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
mno.getPrinters();
complete method:
public String print(){
statements
};
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You always invoke an instance method by saying
someObject.someMethod(...);
where you replace "..." by the actual parameters to this method.
If it's a static method, you can say
SomeClassName.someMethod(....);
instead.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic