简体   繁体   中英

How to call a method in a different Java File?

So I'm pretty new to Java and following a course to learn some coding. Now for this course, I've got a book with theory and exercises. Now I'm stuck at a certain exercise but the book doesn't provided theory or answers to this exercise.

So I've got this Class file ( DateFirstTry.java ) and a Main file, which runs the program.

public class DateFirstTry {

    public String month;
    public int day;
    public int year;
    
    public void writeOutput()
    {
        System.out.println(month + " " + day + ", " + year);
    }
    
    public void makeItNewYears()
    {
        month = "January";
        day = 1;
    }
   
    public void yellIfNewYears()
    {
        if ( (month.equalsIgnoreCase("January") ) && (day == 1) ) 
            System.out.println("Hurrah!");
        else 
            System.out.println("Not New Year's Day.");
    }
}

public class DateFirstTryDemo {

    public static void main(String args[]) {
        DateFirstTry date1, date2;
        date1 = new DateFirstTry();
        date2 = new DateFirstTry();
        date1.month = "December";
        date1.day = 31;
        date1.year = 2012;
        System.out.println("date1:");
        date1.writeOutput();
    
        date2.month = "July";
        date2.day = 4;
        date2.year = 1776;
        System.out.println("date2:");
        date2.writeOutput();
    
        DateFirstTry makeItNewYears;
    
        DateFirstTry yellIfNewYears;
}

So, they explained how I should create the makeItNewYears and yellIfNewYears methods, but don't explain how to call them in the main code. I've tried somethings and DateFirstTry makeItNewYears; is the only thing that doesn't give an error, but also doesn't give any output.

I hope someone can help me with this!

A class can have a state and behavior. State is data. In your class DateFirstTry , these are the variables contributing to the state:

    public String month;
    public int day;
    public int year;

Methods represent a specific behavior or a set of operations. The behavior of the class performs action on the data. It may or may not change the state of the class while performing these actions. For instance,

public void makeItNewYears()
{
    month = "January";
    day = 1;
}

the method makeItNewYears() changes the state, since it changes the value of month and day . On the other hand,

public void writeOutput()
{
    System.out.println(month + " " + day + ", " + year);
}

the method does some action but does not change state. It just uses the data but does not change its value.

An object is an instance of this class. If Person is a class, then John is an instance of this class. A class can have any number of instances. Every instance has its own state and all the behavior defined in the class. To create an instance, you use the following syntax:

DateFirstTry date1 = new DateFirstTry();
DateFirstTry date2 = new DateFirstTry();

date1 and date2 are two instances/objects of this class. They have the same properties month , day and year . But their values can be different for each object. For calling a method, you need to call it on an instance. We have two instances date1 and date2 . For calling writeOutput() use:

date1.writeOutput()

You can call the same method on date2 as well, it will perform the operation on its data.

Each object is able to perform this behavior.

date1.yellIfNewYears()

will check if the values of month, day and year of date1 is New Years. If you call the same method as:

date2.yellIfNewYears()

it will perform this operation with the month, date and year value in date2 .

Since these are instance methods, you first need to create an instance of DateFirstTry , like eg, you did with date1 :

DateFirstTry someDate = new DateFirstTry();

You can then call methods on this instance using the . syntax, followed by the method's name, and parentheses ( () ) containing the arguments you want to pass to the method (in this case - none):

someDate.makeItNewYears();
someDate.yellIfNewYears();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM