• 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

Calling a method

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a real simple questions.
Once I have formed or created a method to return a value, how the heck do I call it in another method I developed...in the same class?
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just call it and catch the returned value like this:
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eddie,
I had a similar point of confusion when I was first learning java. My question was: "if you always need an object to call a method, how do you just call another method in the same class, whether or not you have an object of that class?"
It turns out there are a couple of key java concepts that you need to know:
1. There are two types of methods, static and instance. An instance method requires an object of the class, a static method does not. For example, the Math class (java.lang.Math) has mathematical functions such as sin, cos, tan. You don't need a Math object to call these methods (in fact, the design of the Math class doesn't allow the creation of Math objects) but that's ok since these methods are defined as "static". You call them using the class name, not an object name:
double result = java.lang.Math.sin(someAngle);
2. When you call an instance method, you DO need an object such as:
String fullName = firstName.concat(lastName);
in this case, fullName is a String object and the concat method is an instance method of the String class.
3. If you are calling a method defined in the same class, you can omit the class or object reference.
For example, if your class BankAccount has a method "deposit()", you can call it from within the method applyInterest()":
public void applyInterest(double interestAmt){
deposit(interestAmt);
}//public void applyInterest
even though deposit requires an instance, you don't have to explicitly use it, because applyInterest requires an instance so deposit will get it automatically.
If applyInterest were static, then this wouldn't work since there would be no instance available.
Tony
reply
    Bookmark Topic Watch Topic
  • New Topic