• Post Reply Bookmark Topic Watch Topic
  • New Topic

When do we invoke a method using a dot notation?

 
zipho
Greenhorn
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:
mno.getPrinters();
complete method:
public String print(){
statements
};
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • 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.
 
Jim Yingst
Wanderer
Sheriff
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!