Results 1 to 4 of 4

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

  1. #1
    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.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    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));
    }
    }

  3. #3
    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?

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    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");
    
          }
           
    
          }

Similar Threads

  1. Method overriding versus method hiding in C#
    By ^MALARVIZHI^ in forum Software Development
    Replies: 4
    Last Post: 25-12-2010, 06:25 AM
  2. How to call a method in another class in Java
    By kannudhal in forum Software Development
    Replies: 4
    Last Post: 28-10-2010, 09:29 AM
  3. How can I call VBA method dynamically?
    By opaper in forum Software Development
    Replies: 5
    Last Post: 28-01-2010, 09:25 AM
  4. Is it possible to call destroy() method within init() Method?
    By Level8 in forum Software Development
    Replies: 3
    Last Post: 10-12-2009, 08:36 AM
  5. What is method overriding and method overloading in java
    By beelow in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 08:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,465,883.29269 seconds with 17 queries