Results 1 to 5 of 5

Thread: How to call a method in another class in Java

  1. #1
    Join Date
    Oct 2010
    Posts
    6

    How to call a method in another class in Java

    I have recently started working JAVA in a college assignment on which I'm the only one working with. I cannot wait and am desperate to solve this problem as soon as possible. What I have done until now is that I have created two classes and a main method. The classes are named as class 1 and class 2. Main method belongs to class 1, it is really irrelevant. How do I call a method I've made in class 1 in class 2?

  2. #2
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to call a method in another class in Java

    It depends on what you want to do. If you have class A and class B you can do it like this:

    In Class A:
    Code:
    B myclassB = new B();
    myclassB.mymethod();
    Alternatively, if class B is static:

    In Class B:

    Code:
    public static int add(int a, int b)
    {
       return a+b;
    }
    In Class A:

    Code:
    int sum = B.add(3, 4); // returns 7
    A bit difficult to be brief and specific in such a general question.

  3. #3
    Join Date
    Oct 2010
    Posts
    6

    Re: How to call a method in another class in Java

    Hmm, sorry, I am pretty new to this view. Will try to explain a little further. In class A I have created a method called example.:

    Code:
    public void readText()
    {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What is the headline?");
    title = keyboard.nextLine();
    System.out.println("How many words are there in the text?");
    words = keyboard.nextDouble();
    ..... etc.
    }
    While I have a class B who want to use this method, that is information I got from this method. I do not know if I explained it a bit better now. And it is not a static, I'll use it in a void, though I have not made mistakes. It is also not a main method. These two classes are not a main method, is it then possible to call the method lies in one class, so it can be used in the other class?

  4. #4
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to call a method in another class in Java

    I still not got what you are trying to achieve? In Class B, you could write:

    Code:
    Class A a = new Class A ();
    a.readtext();
    This will run readtext() on A. I think you have to try to explain one more time me what are you trying to achieve? Strange as you've found you there

  5. #5
    Join Date
    Nov 2008
    Posts
    996

    Re: How to call a method in another class in Java

    You do not have a general main method to call the method, it can be done from a method in a class that has access. To invoke this method, you must, in any class, do:

    Code:
    Class A kA = new Class A();
    kA.readtext();
    You will not like that immediately return any results because the method is void. But you can extract values from kA after you've run readtext()

    Code:
    int wordsCount = kA.words:
    string title = kA.header;
    Strictly speaking, you should have get-Class A methods in order to avoid using local variables directly.

Similar Threads

  1. Converting java class to java bean class
    By Ucchal in forum Software Development
    Replies: 6
    Last Post: 09-08-2010, 10:24 AM
  2. Unable to call class into main method in c# code.
    By Kasper in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 07:02 PM
  3. 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
  4. 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
  5. Java: How can I call one method in another method?
    By biohazard 76 in forum Software Development
    Replies: 3
    Last Post: 16-07-2009, 07:12 PM

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,710,828,088.35338 seconds with 17 queries