Results 1 to 9 of 9
- 09-07-2009, 07:50 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
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.
- 09-07-2009, 08:06 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
if the method is static, you can call method by Class.method() like Integer.toString(123)
- 09-07-2009, 08:59 AM #3
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
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,
- 09-07-2009, 09:02 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You should be reading Sun's tutorial for those things.
Generally you should be avoiding static methods in OOP.
- 09-07-2009, 09:57 AM #5
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
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); }
- 09-07-2009, 11:47 AM #6
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
how do setStart related to addDate?
- 09-07-2009, 12:14 PM #7
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
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.
- 09-09-2009, 03:37 AM #8
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
add input parameters to setStart method, pass year, month and day inputs
- 09-09-2009, 07:29 AM #9
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:
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.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"; } }
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
-
how to call method?
By leapinlizard in forum New To JavaReplies: 9Last Post: 04-29-2009, 11:55 PM -
How to call a class within a method
By Manfizy in forum New To JavaReplies: 3Last Post: 03-19-2009, 12:34 PM -
How to call method in servet by using JSP?
By frankjava1 in forum Java ServletReplies: 2Last Post: 10-24-2008, 04:20 AM -
[SOLVED] Call method from another class name
By antgaudi in forum New To JavaReplies: 3Last Post: 10-15-2008, 12:33 AM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks