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,140 points
28 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,400 points

Related Questions In Selenium

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,590 points
27 views
+1 vote
1 answer
0 votes
1 answer

How to get the text from a website using selenium?

hey, you can use get.text() method to ...READ MORE

answered Dec 31, 2018 in Selenium by Sudhanshu
65 views
0 votes
1 answer

How to pass parameters to a Junit test case from the Ant build tool?

The JUnit task accepts nested system property elements. <junit fork="no"> <sysproperty ...READ MORE

answered Mar 6 in Selenium by Vaishnavi
• 720 points
17 views
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,030 points
61 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
432 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
49 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,400 points
54 views
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.