Accessing a method in a different class

Let's say we face a situation where we are working with one class and we need to access an object in another class; this can be done in Java. Let's use an example to help explain this. Let's use two classes, Firstclass() (from the Accessing an object in Java section ), and we'll create a new class, called secondclass(). On creating a new class, the default code is created by the editor and we're able to add code in it. We add a random method, public void setData(), within which we print the I am in second class method statement.

Now, we want the setData() method in the Firstclass() class. Basically we want to execute the setData() method in Firstclass(). And methods can be called only with the objects ...

Get Hands-On Automation Testing with Java for Beginners now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.