Results 1 to 7 of 7
  1. #1
    Hisham is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default How can I call method from class in other class??

    I'm very beginner at java ....

    Just wanna make a class that do some simple operation like add two numbers and call this method from the main class..

    can anyone guide me.??

  2. #2
    o0oNorao0o's Avatar
    o0oNorao0o is offline Member
    Join Date
    Oct 2009
    Posts
    51
    Rep Power
    0

    Default

    I'm beginner too
    but I can help you a little bit
    I always do it like this way

    This an example (Math class)

    Java Code:
    import java.util.Scanner;
    public class Math 
    {
    	Scanner abc = new Scanner(System.in);
    	int number;
    	
    	public void ReadNumber()
    	{
    	    System.out.print("Enter a number :");
    		number = abc.nextInt();
    	}
    	public void Find()
    	{
    		if (number%2==0)
    		{
    			System.out.println(number + "is even");
    		}
    		else
    		{
    			System.out.println(number + "is odd");
    			
    		}
    		
    	}
    	public void Sequare()
    	{
    		System.out.println("The sequare is " + (number*number));
    	}
    	
    		public void Cube()
    	{
    		System.out.println("The cube is " + (number*number*number));
    	}
    	
    }
    here I call the methods in another class (test class)

    Java Code:
    public class test11
    {
    	public static void main(String []args)
    	{
    		Math m = new Math();
    		m.ReadNumber();
    		m.Find();
    		m.Sequare();
    		m.Cube();
    	}
    }
    I hope that would help you :)
    Nora,

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Hisham View Post
    I'm very beginner at java ....

    Just wanna make a class that do some simple operation like add two numbers and call this method from the main class..

    can anyone guide me.??
    Hello Hisham, and welcome to the forum. If Nora's advice doesn't help you, you may want to divulge more information about your problem and even show us your code. If you do post code, please don't forget to use code tags so that the forum's software will have your code maintain its formatting. Please read my signature below on how to achieve this. Much luck!

  4. #4
    Hisham is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks a lot Nora this was Great ....I did it the same way almost.

    and thank you too Fubarable ...I'm very glad to join that great community.

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Also, what Nora showed, was a non-static (aka object) method, you can also make static (aka class) method, which doesn't need to be called from an object instance. For example:
    Java Code:
    public class StatDemo {
         private Sting name;
         private static num = 0; //this variable is static, all objects of this class share the same value, while the name string will be unique to each object
         public StatDemo(String name) {
              this.name = name;
              num++;
         }
      
         public void printName() { //non-static function, each object produces different output
              System.out.println(name);
         }
         
         public static void printNum() {
               System.out.println(num);
         }
    }
    public class TestStat {
         public static void main(String[] args) {
               StatDemo s1 = new StatDemo("Dave"),
               s2 = new StatDemo("Steve"),
               s3 = new StatDemo("John"); //each time we create a new StatDemo object, it's static variable increases
               s1.printName(); s2.printName(); s3.printName();
               StatDemo.printNum();
         }
    }
    Note that the non-static calls were preceded by an object instance, while the static method call was preceded by the class name. The output of this program is:
    Java Code:
    Dave
    Steve
    John
    3

  6. #6
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    m00nchile, thanks for that info.
    I didn't knew it!!!!!
    thank you verry much

  7. #7
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

Similar Threads

  1. how to call higher level super class method?
    By satheeshtech in forum Advanced Java
    Replies: 2
    Last Post: 01-12-2010, 03:11 PM
  2. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  3. How to call a method from another class?
    By jboy in forum New To Java
    Replies: 8
    Last Post: 09-09-2009, 07:29 AM
  4. How to call a class within a method
    By Manfizy in forum New To Java
    Replies: 3
    Last Post: 03-19-2009, 12:34 PM
  5. [SOLVED] Call method from another class name
    By antgaudi in forum New To Java
    Replies: 3
    Last Post: 10-15-2008, 12:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •