Google Answers Logo
View Question
 
Q: Accessing object from a different method (JXL package used) ( Answered 4 out of 5 stars,   0 Comments )
Question  
Subject: Accessing object from a different method (JXL package used)
Category: Computers > Programming
Asked by: gooseman-ga
List Price: $6.00
Posted: 31 Aug 2002 17:47 PDT
Expires: 30 Sep 2002 17:47 PDT
Question ID: 60588
I've got this Java code, using the jxl package (although the problem
is a Java, so no knowledge of JXL is nec)

public class dataOutput
{
  public static boolean spreadsheetCreated = false;
  private static String fileName;

  public static void storeConfigData()
  {
    fileName = "outputRun"+simulationControl.scenarioCounter+".xls";
    // If file not opened yet, open
    if (spreadsheetCreated == false)
    {
      try
      {
        // Create the spread sheet
        WritableWorkbook workbook = Workbook.createWorkbook(new
File(fileName));
        spreadsheetCreated = true;

        // Name the first sheet in the file for the configuration data
        WritableSheet configSheet = workbook.createSheet("Sim"
           +simulationControl.scenarioCounter+" Config", 0);
====SNIP====

Now what I want to do is to access configSheet from a different
method, but I can't. How do I do this? JXL can be downloaded at
http://www.andykhan.com/jexcelapi/

Clarification of Question by gooseman-ga on 31 Aug 2002 17:57 PDT
Forgot to mention - JXL is a package that allows you to read and write
to excel spreadsheets.
Answer  
Subject: Re: Accessing object from a different method (JXL package used)
Answered By: answerguru-ga on 31 Aug 2002 19:14 PDT
Rated:4 out of 5 stars
 
Hi gooseman-ga,

The problem you are encountering is one of variable scope -
configSheet is a WritableSheet object which currently has a scope the
size of the  storeConfigData() method. In other words, you are
currently only able to access this variable within this method.

There are two possible ways around this:

1. Change the signature of your storeConfigData() method so that it
returns a WriteableSheet instead of nothing (represented by "void").
Remember that Java has automatic garbage collection so you don't need
to worry about this object being returned in your main() method unless
you want to use it - in that case you would save it to a variable in
main() and proceed.

// below is the implementation of solution #1

public class dataOutput 
{ 
  public static boolean spreadsheetCreated = false; 
  private static String fileName; 
 
  public static WritableSheet storeConfigData() 
  { 
    fileName = "outputRun"+simulationControl.scenarioCounter+".xls"; 
    // If file not opened yet, open 
    if (spreadsheetCreated == false) 
    { 
      try 
      { 
        // Create the spread sheet 
        WritableWorkbook workbook = Workbook.createWorkbook(new 
File(fileName)); 
        spreadsheetCreated = true; 
 
        // Name the first sheet in the file for the configuration data
        WritableSheet configSheet = workbook.createSheet("Sim" 
           +simulationControl.scenarioCounter+" Config", 0); 

        /* the rest of your method code here */
        return configSheet;
====SNIP==== 


2. Create a global WritableSheet object and use this to store the
configSheet variable.

// below is the implementation of solution #2

public class dataOutput 
{ 
  public static boolean spreadsheetCreated = false; 
  private static String fileName; 
  public static WritableSheet globalSheet;
 
  public static void storeConfigData() 
  { 
    fileName = "outputRun"+simulationControl.scenarioCounter+".xls"; 
    // If file not opened yet, open 
    if (spreadsheetCreated == false) 
    { 
      try 
      { 
        // Create the spread sheet 
        WritableWorkbook workbook = Workbook.createWorkbook(new 
File(fileName)); 
        spreadsheetCreated = true; 
 
        // Name the first sheet in the file for the configuration data
        WritableSheet configSheet = workbook.createSheet("Sim" 
           +simulationControl.scenarioCounter+" Config", 0); 

        globalSheet = configSheet;   // storing configSheet in the
global var

====SNIP==== 


So which of these two approaches is better? Personally I would go with
the first one for the following reasons:

1. You don't know how many times this method is going to be called, so
if it is called multiple times the global variable will only retain
the last WritableSheet. By returning the variable to the calling
procedure each time, you can let the author of that procedure decide
whether or not any given variable is worth storing.

2. Global variables, while legal, are considered bad style. When
coding any class, you have to assume that others will be using it for
a variety of purposes that you are not able to predetermine.

3. The creators of Java have made it easy with garbage collection -
you don't even need to account for returned variables that are not
being stored in the calling procedure.

CONCLUSION: Return a WriteableSheet by using the modified code used in
the first solution.


Hope that clears things up...let me know if you have problems
understanding the above information by posting a clarification.

Cheers!

answerguru-ga
gooseman-ga rated this answer:4 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy