• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instance methods...

 
Dan Silva
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is an instance method? Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An ordinary method.
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An instance method requires an instance of the object to be called. The opposite of an instance method is a static method. If you use the qualifier "static" when you define a method, it is a static method instead of an instance method.

Instance methods can access instance variables and they can access static variable (also known as class variables because there is only one static variable for the entire class regardless of which instance you are using).

Static methods can only access static variable.

The most famous static method is the main() method.

Also, if you look at the API JavaDoc, the Math class is made up of all static methods. For example, if you want to calculate the square root of a primitive. There is no object, so rather than requiring you to create an object, you call:

int x = 144;
Math.sqrt(x);

In the above code, instead of using an instance, you use the name of the class, "Math" because there is no instance of an object.

Again, if you leave off the static qualifier, you get an instance method as you are calling it or a regular method as Campbell is calling it.
 
Dan Silva
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I knew it was something simple, but I just wasn't sure if there was something weird going on.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic