Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

using a static method in a different class

Let us suppose we have a class:

package package1;

public class Car {
    public static int brake()
    {
         int x;
         //some functionalities
    }
    //other functionalities
}

I want to ask for using this method brake in classes of different package, do we need to include package name also? — int xyz=package1.Car.brake(); or simply Car.brake(); will work

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can import package or use full path of method:

First solution:

public class App {

    public static void main(String[] args) {
        package1.Car.brake();
    }

}

Second solution:

import package1.Car;

public class App {

    public static void main(String[] args) {
        Car.brake();
    }

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading