简体   繁体   中英

Accessing the method from another class in java

I have added Checkgateway class (converted has jar)to the build path of another class verifyLogin

public class Checkgateway {
    public static void GetDetail(String n) {
        final File plugin_list = new File("C:\\Users\\RajendraPrasadH\\eclipse-workspace\\Gateway\\src\\config.cfg");
    
            PluginLoader.parseConfig(plugin_list, null);
}

I need to access GetDetail() method in verifyLogin.java without extending this class but importing this class can be done.Is there any method to use GetDetail() method in verifyLogin.java without extending Checkgateway class.

I assume that GetDetail(String n) is not an instance method, so yo can import the class in the top of the verifyLogin.java class like import thePathOfTheClass.CheckGateway , and then you can call the method like Checkgateway.GetDetail("Example")

I may not have get your question correctly, but by importing

  1. you can directly call static method on class names

    Checkgateway.GetDetail("Example")

  2. Or instance methods by instantiating class and calling method on instance

    Checkgateway gateway = new Checkgateway();

    gateway.GetDetail("Example"); //in case GetDetails is an instance method

The method getDetail() is static

In the Java programming language, the keyword static indicates that the particular member belongs to a type itself, rather than to an instance of that type. This means that only one instance of that static member is created which is shared across all instances of the class.

You should remove static keyword as, from what I can tell from your example you don't need all the instances of CheckGateWay to share the same instance of the member function.

After you do this import the class import. …. ….CheckGateway import. …. ….CheckGateway import. …. ….CheckGateway on the top of your verifyLogin.java file.

Create a new instance of the class in verifyLogin.java. ie CheckGateWay chkGateWay = new CheckGateWay();

Then just call the method ex. chkGateWay.getDetail("String");

Hope this helps.

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