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

3
  • 4
    You understand that courseName in your SecondCourse class is a Course, not a CharSequence, right? Commented May 12, 2014 at 18:07
  • can you explain it a bit.. I am not a geek or expert in java :) (just a beginner). Where should I edit? Commented May 12, 2014 at 18:26
  • Can you look into my answer below. Commented May 12, 2014 at 18:30

5 Answers 5

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 
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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"; "
The class does not have to be static to use a static variable Understanding Class Members
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.

Comments

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);
}

Comments

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.

Comments

-2

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

4 Comments

courseName is not a static member of Course. You won't be able to call it like that.
You really, really, really shouldn't make member variables static just to make them easier to call. That violates the whole point of encapsulation.
okay ..thanks..for d info..will.keep in mind..ten wat is d other was to access it..
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.