Results 1 to 9 of 9
  1. #1
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default How to call a method from another class?

    Hi,

    I just need to know what options I have to call a method from another class? because my program consists of multiple classes and the only way that I think of calling a method is to create an instance of a class and then call the class method using the instance.mehod. Are there any other ways that I can call methods from a different class?

    Thank you.

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    8

    Default

    if the method is static, you can call method by Class.method() like Integer.toString(123)

  3. #3
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    Oh, really. Thanks heaps for pointing this out for me, I always thought there must be a way we could call a method from other classes and now I have a better control over using the methods from other classes because I don't need to create an object of a class anymore. But what is the purpose of static method? Is there any risks involved? Also, is there any other way that I am unaware for calling methods?

    Thanks again,

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    You should be reading Sun's tutorial for those things.
    Generally you should be avoiding static methods in OOP.

  5. #5
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    I have a small problem with my following code where I am trying to access the static method in the root class. I don't know how to relate date.add(..) in the addDate method in my other class to the root class.

    Java Code:
    This is my root class:
    
    import java.text.*;
    import java.util.*;
    
    public class Car
    {  private static GregorianCalendar date = new GregorianCalendar();
        public static void main(String[] args)
       { new Car();
          date.setLenient(false);}		
    
          public static void setStart()
          {	date.set(GregorianCalendar.YEAR, 2008);
        	date.set(GregorianCalendar.MONTH, 1);
        	date.set(GregorianCalendar.DATE, 1);   	}
    
    }
    
    This is a piece of my other class:
    
    	private String getDate()
    	{   System.out.print("\t\t\tDay: ");
    	    day = In.nextInt();
    	    System.out.print("\t\t\tMonth: ");
    	    month = In.nextInt();
    	    System.out.print("\t\t\tYear: ");
    	    year = In.nextInt();
    	    return addDate(day, month, year);	}
    
    	private String addDate(int day, int month, int year)
    	{	date.add(date.get(5), day);
    		date.add(date.get(2)+1, month);
    		date.add(date.get(1), year);
    		return sdf.format(date);	}

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    8

  7. #7
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    That is what I need to do that is to relate addDate to setStart. The program reads the integar numbers entered by the user and add them to the setStart date.

  8. #8
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    8

    Default

    add input parameters to setStart method, pass year, month and day inputs

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,327
    Rep Power
    9

    Default

    also, if all your methods are private, you cannot call them from another class if that class is in a separate file. I suggest you don't use static at this point until you are more comfortable with the language.

    Why not use a driver style structure? Class Driver contains an instance of class A and Class B, and you can give a reference of A to B or vice versa, or not at all. For example:
    Java Code:
    public class Driver {
        public static void main(String[] args) {
            new Driver();
        }
    
        public Driver(){
            B b = new B();
            A a = new A(b);
            System.out.println(a.getGreeting());
            System.out.println(b.getName());
        }
    }
    
    public class A{
        private B b;
        
        public A(B b){
            this.b = b;
        }
    
        public String getGreeting(){
            return "Hello " + b.getName();
        }
    }
    
    public class B{
        public String getName(){
            return "Ralph";
        }
    }
    In this example, the driver is the main portion of the program. It controls everything. I created an instance of class B and then an instance of class A. Class A takes a reference to class B in it's constructor. In A, you can see that it calls a method that belongs to B. You can also see that I call a method in B directly from the Driver. You can rearrange this setup any way you please.

    When I code, I tend to have many independent objects (separate class files) that are all linked together with a Driver of sorts. If two different classes need to talk to eachother, they can either do it through the Driver with accessor methods, or directly by storing a reference to each other. Here is another example using accessor methods in the Driver:

    Java Code:
    public class Driver {
        public static void main(String[] args) {
            new Driver();
        }
        
        private B b;
        private A a;
        
        public Driver(){
            b = new B();
            a = new A(this);
        }
        
        public void tellBToPrint(){
            b.printSauce();
        }
    }
    
    public class A{
        public A(Driver driver){
            printTaco();
            driver.tellBToPrint();
        }
        
        public void printTaco(){
            System.out.println("Crunch taco");
        }
    }
    
    public class B{
        public void printSauce(){
            System.out.println("Spicy");
        }
    }

Similar Threads

  1. how to call method?
    By leapinlizard in forum New To Java
    Replies: 9
    Last Post: 04-29-2009, 11:55 PM
  2. How to call a class within a method
    By Manfizy in forum New To Java
    Replies: 3
    Last Post: 03-19-2009, 01:34 PM
  3. How to call method in servet by using JSP?
    By frankjava1 in forum Java Servlet
    Replies: 2
    Last Post: 10-24-2008, 04:20 AM
  4. [SOLVED] Call method from another class name
    By antgaudi in forum New To Java
    Replies: 3
    Last Post: 10-15-2008, 12:33 AM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 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
  •