How to Call a Class From another class?

0 votes

I want to call class2 from class1 but class2 doesn't have a main function to refer to like Class2.main(args);

How can I call class2 from class1?

Aug 8, 2018 in Selenium by darklord
• 6,170 points
38 views

1 answer to this question.

0 votes

Suppose you have two classes:

Class1:

public class Class1 {
    //Your code above
}

Class2:

public class Class2 {
}

You can use Class2 in different ways:

Class Field:

public class Class1{
    private Class2 class2 = new Class2();
}

Method field:

public class Class1 {
    public void loginAs(String username, String password)
    {
         Class2 class2 = new Class2();
         class2.invokeSomeMethod();
         //your code
    }
}

Static methods from Class2 Imagine this is your Class2:

public class Class2 {
     public static void doSomething(){
     }
}

From class1 you can call Class2 :

public class Class1 {
    public void loginAs(String username, String password)
    {
         Class2.doSomething();
         //your code
    }
}
answered Aug 8, 2018 by Meci Matt
• 9,420 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer

How to choose from a drop-down in Selenium 2?

You can probably use the 'Select' class ...READ MORE

answered Apr 24, 2018 in Selenium by king_kenny
• 3,650 points
126 views
+1 vote
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
750 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
995 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
708 views
0 votes
1 answer

How to download a pdf file diagrammatically from a web page with .html extension?

For downloading a pdf file: import java.io.File; import java.io.FileOutputStream; import ...READ MORE

answered Sep 28, 2018 in Selenium by Meci Matt
• 9,420 points
98 views
0 votes
1 answer