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;
}
}
enter link description hereYou need to understand the difference between classes and objects. From the Java tutorial:
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.
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:
B, which contains the doSomething() method would look like this:
You need to instantiate the other classes inside the main class;
You can then call their methods with:
You currently have constructors in your other classes. You should not return anything in these.
Next you need a method to reference.
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.
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 -
In class
WeatherRecord
:First import the class if they are in different package else this statement is not requires
Then, just referene or call your object like:
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.
Have clear concept on Object and Static functions. Click me