1

I ran into an issue, where I need to call a variable created in one java class in another java class.

Course.java

public class Course {
   public String courseName;

SecondCourse.java

    public class SecondCourse {
        Course courseName;

@Before
    public void setUp() throws Exception {
        courseName = new Course();

Eventhough, I have set up it like this, the varible call doesn't work in SecondCourse.java Class. Have I missed something? Can you help me?

I'm trying to call

driver.findElement(By.xpath("//div/input")).sendKeys(courseName); 

in the SecondCourse.java class. But gives me the error sendKeys(java.lang.CharSequence...) in org.openqa.selenium.WebElement cannot be applied

Harshini
  • 353
  • 2
  • 6
  • 12

5 Answers5

1

First of all you define in your Course.java class

public class Course
{
    public static String courseName; //Define variable as static here
 }

Access that variable in any class using class name

Course.courseName = "abc"; // /Access this variable 
Bhadresh
  • 115
  • 2
  • 3
  • 13
  • You can't do it this way, only if Course class is static, then only you can call a class variable directly using class Name. – RIP SunMicrosystem May 12 '14 at 18:42
  • Even if you declare the variable static it won't help, if the clas is static then only you can use this "Course.courseName = "abc"; " – RIP SunMicrosystem May 12 '14 at 18:49
  • The class does not have to be static to use a static variable [Understanding Class Members](http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) – lgraham076 May 12 '14 at 18:57
0

Course courseName of SecondCourse defines a member field of type Course - it does not correspond to String courseName of Course. You can, if you want, access the courseName string of the Course object stored in courseName Course of SecondCourse using courseName.courseName.

Idan Arye
  • 12,402
  • 5
  • 49
  • 68
0

Simply In SecondCourse.java

       public class SecondCourse {
            Course obj = new Course(); // creates new obj for Course class

    @Before
        public void setUp() throws Exception {
            //courseName = new Course(); //You won't need this.
        system.out.println("Here is how your obj can be called "+ obj.courseName);
}
0

Not 100% sure what your overall goal is, but this might prove helpful.

public class Course {
    private String courseName;

    public Course(String courseName) {
        this.courseName=courseName;
    }

    public String getCourseName() {
        return this.courseName;
    }
}

public class SecondCourse {
    private Course course;

    public SecondCourse(String courseName) {
        course=new Course(courseName);
    }

    public void setup() {
        String courseName=course.getCourseName();
        /*
         * Do something with courseName
         */
    }
}

The Course class initialize and grants access to it's courseName variable and the Second Course does additional work on the course using the functionality given by the Course class.

In addition this post on encapsulation may prove useful.

Community
  • 1
  • 1
lgraham076
  • 81
  • 8
-2

Use Course.courseName, u will be able to access the other class variable. Hope it helps. Thanks.

Akash Goswami
  • 306
  • 3
  • 14
  • 4
    `courseName` is not a static member of `Course`. You won't be able to call it like that. – azurefrog May 12 '14 at 18:13
  • 3
    You really, *really*, **really** shouldn't make member variables static just to make them easier to call. That violates the whole point of encapsulation. – azurefrog May 12 '14 at 18:19
  • okay ..thanks..for d info..will.keep in mind..ten wat is d other was to access it.. – Akash Goswami May 12 '14 at 18:22
  • 1
    @AkashGoswami It violates more than just encapsulation - it violates the logic of the program. The question is not about how you access that variable - it's about whether or not you want each instance of the class to have it's own value for that field. – Idan Arye May 12 '14 at 18:45