9

I'm writing a java project that has three different classes. This is what i have have so far. I'm just stuck on how do you call a method function from another class to another class. I have written 2 classes already. I got the "Date" class and "TemperatureRange" class done; now i'm trying to call those 2 classes into "WeatherRecord" class. I'm not sure if i'm explaining this right.

public class WeatherRecord //implements Record
{

    private String TemperatureRangetoday;
    private String TemperatureRangenormal;
    private String TemperatureRangerecord;


    public static void main (String[] args){

    }
}

This is another class

public class Date
{
    public static String date(String date, String month, String year){
        String rdate = date + " " +month + " " +year;
        return rdate;   
    }
}

And here's another class

public class TemperatureRange
{
    public static String TempRange (String high, String low){

        String rTempRange = high +"high" + " "+low+"low";
        return rTempRange;

    }
}
Andy
  • 699
  • 7
  • 18
Chase
  • 99
  • 1
  • 1
  • 6

6 Answers6

21

You need a reference to the class that contains the method you want to call. Let's say we have two classes, A and B. B has a method you want to call from A. Class A would look like this:

public class A
{
    B b; // A reference to B

    b = new B(); // Creating object of class B

    b.doSomething();  // Calling a method contained in class B from class A
}

B, which contains the doSomething() method would look like this:

public class B
{
    public void doSomething()
    {
        System.out.println("Look, I'm doing something in class B!");
    }
}
user2713461
  • 377
  • 1
  • 5
  • 16
MarsAtomic
  • 9,672
  • 5
  • 32
  • 55
  • On that note, I don't think he should be using static methods since he's creating instances of Date and TemperatureRange. Static methods will mean they will belong to the class they are in, not the instance. – tenkii Oct 09 '14 at 02:13
  • I suspect that he ran into some issues attempting to call non-static methods from within his main method. I wonder if my example is too general and I should have customized it specifically for his code, but I'm always wary of spoon feeding... – MarsAtomic Oct 09 '14 at 02:15
  • Is this the design pattern that is referred to as dependency injection? Or is that something completely unrelated? – random_coder_101 Jul 21 '17 at 11:41
  • 1
    @ZaidHumayun The code I gave demonstrates a simple dependency. Dependency injection takes a few different forms, but in this case, involves passing a reference to an existing class B to class A. For example, if class A had the constructor `public A(B instanceOfB) { this.b = instanceOfB }`. The term `injection` indicates a pushing of something (in this case an object reference) into something else (another object). – MarsAtomic Jul 24 '17 at 02:24
3

In class WeatherRecord:

First import the class if they are in different package else this statement is not requires

Import <path>.ClassName



Then, just referene or call your object like:

Date d;
TempratureRange tr;
d = new Date();
tr = new TempratureRange;
//this can be done in Single Line also like :
// Date d = new Date();



But in your code you are not required to create an object to call function of Date and TempratureRange. As both of the Classes contain Static Function , you cannot call the thoes function by creating object.

Date.date(date,month,year);   // this is enough to call those static function 


Have clear concept on Object and Static functions. Click me

Asis
  • 545
  • 2
  • 16
1

For calling the method of one class within the second class, you have to first create the object of that class which method you want to call than with the object reference you can call the method.

class A {
   public void fun(){
     //do something
   }
}

class B {
   public static void main(String args[]){
     A obj = new A();
     obj.fun();
   }
}

But in your case you have the static method in Date and TemperatureRange class. You can call your static method by using the class name directly like below code or by creating the object of that class like above code but static method ,mostly we use for creating the utility classes, so best way to call the method by using class name. Like in your case -

public static void main (String[] args){
  String dateVal = Date.date("01","11,"12"); // calling the date function by passing some parameter.

  String tempRangeVal = TemperatureRange.TempRange("80","20"); 
}
halapgos1
  • 1,052
  • 3
  • 15
  • 31
Moni
  • 433
  • 3
  • 9
0

You need to instantiate the other classes inside the main class;

Date d = new Date(params);
TemperatureRange t = new TemperatureRange(params);

You can then call their methods with:

object.methodname(params);
d.method();

You currently have constructors in your other classes. You should not return anything in these.

public Date(params){
    set variables for date object
}

Next you need a method to reference.

public returnType methodName(params){
  return something;
}
Chad
  • 104
  • 1
  • 2
  • 6
0

You need to understand the difference between classes and objects. From the Java tutorial:

An object is a software bundle of related state and behavior

A class is a blueprint or prototype from which objects are created

You've defined the prototypes but done nothing with them. To use an object, you need to create it. In Java, we use the new keyword.

new Date();

You will need to assign the object to a variable of the same type as the class the object was created from.

Date d = new Date();

Once you have a reference to the object you can interact with it

d.date("01", "12", "14");

The exception to this is static methods that belong to the class and are referenced through it

public class MyDate{
  public static date(){ ... }
}

...
MyDate.date();

In case you aren't aware, Java already has a class for representing dates, you probably don't want to create your own.

tanner
  • 7
  • 4
Romski
  • 1,858
  • 1
  • 12
  • 22
0
import <path>;
 My main java program:

import method;
import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner n = new Scanner(System.in);
        int a,b,c;
        double area,s;
        System.out.print("Enter the 1st side->");
        a = n.nextInt();
        System.out.print("Enter the 2nd side->");
        b = n.nextInt();
        System.out.print("Enter the 3rd side->");
        c = n.nextInt();
        s=(a+b+c)/2.0;
        area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
        System.out.println("Area is = "+area);
        //factorial function is contained in the class method
        System.out.print(method.factorial(a));
    }
}