In Excel handling how is it that we call getRow without creating object of XSSFSheet class

+1 vote
How is it that we invoke getRow() and getCell() without creating object of XSSFSheet class. To create an object we use new keyword right but without using new keyword how is it that we can call the non static methods getRow() and getCell ()

Ex:
XSSFWorkbook workbook = new XSSFWorkbook(fs);
XSSFSheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);

As we can see here we are calling getRow() and getCell() without creating object of XSSFSheet class using new keyword.
Oct 7, 2020 in Selenium by anonymous
• 130 points
487 views

1 answer to this question.

0 votes

In Excel file handling using Apache POI, when you call getRow on an XSSFSheet object, you are indeed using an instance of the XSSFSheet class. However, you might not be directly creating this instance using the new keyword yourself; instead, it's often retrieved from an existing XSSFWorkbook object.

Here's a step-by-step explanation of how this typically works:

  1. Creating an XSSFWorkbook Instance: First, you create an instance of XSSFWorkbook, which represents an entire Excel workbook. This is usually where you use the new keyword, for example, when creating a new workbook or loading an existing one from a file.

    XSSFWorkbook workbook = new XSSFWorkbook(); // for a new workbook // or XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream("path_to_excel_file")); // for an existing workbook 
  2. Retrieving an XSSFSheet Instance: Once you have the XSSFWorkbook instance, you retrieve an XSSFSheet object from it. This doesn't require the new keyword because the XSSFWorkbook class provides methods to access its sheets, either by name or index. These sheets are already part of the workbook structure.

    XSSFSheet sheet = workbook.getSheetAt(0); // retrieves the first sheet 
  3. Using getRow on the XSSFSheet Instance: Now, with the XSSFSheet instance, you can call getRow. The getRow method is an instance method of the XSSFSheet class. It doesn't create a new row but retrieves an existing one from the sheet. If the row doesn't exist, it may return null or create a new row, depending on how the library is implemented.

    Row row = sheet.getRow(0); // retrieves the first row 

In this process, you're not creating a new XSSFSheet object directly; instead, you're obtaining a reference to an existing sheet from the XSSFWorkbook object. The creation and management of the XSSFSheet instances are handled internally by the Apache POI library. This design pattern, where a parent object manages the creation and lifecycle of child objects, is common in object-oriented programming.

answered Oct 7, 2020 by Karan
• 19,610 points
Thank you so much.
Glad that it helped!

Thank you for your contribution to the Edureka Community. Do upvote the answer in case you found it helpful.

Cheers!

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer

What is Base class in Selenium and how it works?

Hey Prashant, in Selenium Base class is the main ...READ MORE

answered Jul 18, 2019 in Selenium by Abha
• 28,140 points
18,838 views
0 votes
2 answers

How can we take screenshots of tests in Selenium 2 using C#

Hey, try using following code command to ...READ MORE

answered Aug 23, 2019 in Selenium by Abha
• 28,140 points
965 views
0 votes
1 answer

How to select an Object by its class in Selenium?

When it comes to Selenium, XPath will ...READ MORE

answered Apr 14, 2018 in Selenium by king_kenny
• 3,710 points
742 views
0 votes
1 answer

Is it possible for a website to detect that we are using Selenium with ChromeDriver

Selenium tests for pre-defined javascript variables which ...READ MORE

answered Apr 28, 2018 in Selenium by Meci Matt
• 9,460 points
5,832 views
0 votes
2 answers

what is the need of xpath when you have attributes like id ,class,name in selenium?

some of the controls not have id ...READ MORE

answered Sep 4, 2020 in Selenium by Sri
• 3,190 points
1,554 views
0 votes
1 answer

How to avoid Compound Class name error in Page Object?

Use a CSS selector instead: .country.name CSS selector is ...READ MORE

answered Jul 31, 2018 in Selenium by Meci Matt
• 9,460 points
966 views
0 votes
1 answer
+1 vote
1 answer

How can I automate the process of adding iPhone to cart in Flipkart using Selenium(java),Page Object Model and TestNG? Also validate if product is added and available in cart?

Hey check this https://www.edureka.co/community/47160/automate-purchase-adding-book-cart-flipkart-using-selenium? It deals with a similar ...READ MORE

answered Jan 13, 2020 in Selenium by Karan
• 19,610 points
7,789 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