krr
krr

Reputation: 55

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

Upvotes: 1

Views: 558

Answers (2)

Mehdi Rahimi
Mehdi Rahimi

Reputation: 2536

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();
    }

}

Upvotes: 2

Prog_G
Prog_G

Reputation: 1615

Add import statement like import package1.Car; and then you can use Car.brake(); to call the function. Read more about imports here

Upvotes: 2

Related Questions