Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: how to invoke a method?????

  1. #1
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default how to invoke a method?????

    hello,

    please tell me how to use the below stated method " Amr(x , y) " in anoth/different .java file.

    public class Amr {
     
        public int Amr(int x, int y) 
        {
        	return (x+y);
     
        }
    }
     
    import ??????????
    public class sum2 {
     
        public static void main(String [] args) 
        {
        	System.out.println( Amr(2,3) );
     
        }
    }
    Last edited by amr; December 3rd, 2010 at 07:40 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,273
    Thanks
    177
    Thanked 822 Times in 765 Posts
    Blog Entries
    5

    Default Re: how to invoke a method?????

    I suggest you read the following tutorial to help you learn to do this: Classes (The Java Tutorials). As is, you can a) call a static method from the main, b) create a new object and call a method of said object, c) do the math in main accessing the values of a created object d) its probable I missed another way. For example, a)
    public class Something{
        public static int sum(int a, int b){
            return a+b;
        }
    }
    //
    Something.sum(1,2);//perform the sum
    Last edited by copeg; December 2nd, 2010 at 09:08 PM.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: how to invoke a method?????

    Two ways.

    One, make it a static method and do this in the main method

    System.out.println(className.Amr(2,3));

    Or use the constructor of your first class to make an object of that class then

    System.out.println(object.Amr(2,3));

Similar Threads

  1. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM