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 Sahiti
• 6,370 points
52,846 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
    }
}

I hope this helps!

Join Selenium training for more information!

Thanks!

answered Aug 8, 2018 by Meci Matt
• 9,460 points

Related Questions In Selenium

0 votes
3 answers

How to print text from a list of all web elements with same class name in Selenium?

Try using List <WebElement> to access all similar elements ...READ MORE

answered Dec 16, 2020 in Selenium by Roshni
• 10,480 points
87,425 views
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,710 points
1,151 views
+1 vote
1 answer
0 votes
2 answers

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,500 points
13,673 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,142 views
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,910 points
7,221 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,874 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,460 points
1,041 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP