Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Java: How can I call one method in another method?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 16-07-2009
Member
 
Join Date: Apr 2008
Posts: 47
Java: How can I call one method in another method?
  

Hi,

Java: How can I call one method in another method? I have a problem calling a method from another class.
Is there any way I can call a method which is defined in another class into my new class?
I know it is possible but I just need to see an example to see it since both the classes are stored in different files.
I need to execute that method since without its execution my program wont work further since its a check of my database.
Please help.

Reply With Quote
  #2  
Old 16-07-2009
Member
 
Join Date: Jan 2008
Posts: 1,514
Re: Java: How can I call one method in another method?

Can you tell us about your project.
Are both classes in the same package or not?

You just need to learn this topic of java tutorial:

Java call method from another class

The class calling the method needs a reference to the other class.

CallingMethod.java
MainClass.java


For getting access to CallingMethod class methods inside the class MainClass, an object of the CallingMethod class of CallingClass.java file has to created inside the class of MainClass.java file as shown in the example.

Syntax of creating object : ClassName objectName = new ClassName();

After creating object of CallingMethod.java file class CallingMethod inside the class of MainClass.java file. Its very easy to access the methods of the class CallingMethod. With just the object name along with a dot operator, any method at a time can accessed

Syntax for calling methods : objectName . methodName();

Note : Base directory of both the java files should match with each other.

Example:

Here is the code of CallingMethod.java

Code:
public class CallingMethod{
int c;
public int add(int a, int b){
return a+b;
}
public int subtract(int a, int b){
return a-b;
}
public int multiply(int a, int b){
return a*b;
}
public int division(int a, int b){
return a/b;
}
public int modulus(int a, int b){
return a%b;
}
}
Here is the code of MainClass.java

Code:
public class MainClass {
public static void main(String[] args){
CallingMethod method = new CallingMethod();
System.out.println("Addition: " + method.add(30,15));
System.out.println("Subtraction: " + method.subtract(30,15));
System.out.println("Multiplication: " + method.multiply(30,15));
System.out.println("Division: " + method.division(30,15));
System.out.println("Modulus: " + method.modulus(30,15));
}
}
Reply With Quote
  #3  
Old 16-07-2009
Member
 
Join Date: May 2008
Posts: 42
Re: Java: How can I call one method in another method?

I have a small question related to calling a method from another class.
My question is can the constructor of my class call another method in the same class.
I want to call the method into the constructor it wont change any member data. Its just due to the part calculate a big calculation so I just want to reuse the code which is written in a different language which will be compiled by a third party API while this object is constructed.
IS this possible?
Reply With Quote
  #4  
Old 16-07-2009
Member
 
Join Date: May 2008
Posts: 2,381
Re: Java: How can I call one method in another method?

Yes you can do it.
What you are trying to do is calling a virtual function from a constructor. You can search for it. It's safe in the sense that the behavior is well defined, and
you can take precautions and handle it. You can do it without much trouble if you can understand what exactly you are doing. In c++ its absolutely easy.

This example shows you how to call a method from another class.
Class #1

Code:
      package basic;
       

      public class MethodCallingA {
       

      public static void main(String[] args){

      MethodCallingB MCB = new MethodCallingB();

      MCB.methodB();

      }
       

      }
Class #2

Code:
      package basic;
       

      public class MethodCallingB {
       

      public void methodB(){

      System.out.println("You called the method: methodB");

      }
       

      }
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Java: How can I call one method in another method?"
Thread Thread Starter Forum Replies Last Post
Method overriding versus method hiding in C# ^MALARVIZHI^ Software Development 4 25-12-2010 05:25 AM
How to call a method in another class in Java kannudhal Software Development 4 28-10-2010 09:29 AM
How can I call VBA method dynamically? opaper Software Development 5 28-01-2010 08:25 AM
Is it possible to call destroy() method within init() Method? Level8 Software Development 3 10-12-2009 07:36 AM
What is method overriding and method overloading in java beelow Software Development 3 17-11-2009 07:20 AM


All times are GMT +5.5. The time now is 03:34 AM.