Hi guys,
I am confused on how can I access a same class reference object , that is created in class ReadInoiceFile, and be accessed in ReadErrorFile.

To be more specific:
I have a class called Transaction

And then I have a class called ReadInvoiceFile.
And finally I have a class called ReadErrorFile.

So if I create a reference object of Transaction in ReadInvoiceFile and use the mutator method setOrderNumber to set a value of a variable called OrderNumber, how can I get access to that value in class ReadErrorFile.

Because if I create a reference object of Transacton in ReadErrorFile and try accessing OrderNumber I get a new copy which initially will be null.

Here is code snippet. I hope my question makes sense and thanks for any help.

public class Transaction
{
    private String InvoiceNumber ; 
    private String OrderNumber;

    public Transaction()
    {
        InvoiceNumber = " ";
        OrderNumber = " ";
    }

    public void setInvoiceNumber(String InvoiceNo)
    {
        this.InvoiceNumber = InvoiceNo;
    }

    public void setOrdrNumber(String OrderNo)
    {
        this.OrderNumber = OrderNo;
    }

    public String getInvoiceNumber()
    {
        return InvoiceNumber;
    }

    public String getOrderNumber()
    {
        return OrderNumber;
    }

    public class ReadInvoiceFile
    {
        Transaction transaction = new Transaction();

        public void ReadInput()
        {
            transaction.setOrderNumber("123AF");
        }
    }

    public class ReadErrorFile
    {
        //this would create complete new object
        Transaction transaction = new Transaction();

        public void getResults()
        {
            //this would print out null
            System.out.println(transaction.getOrderNumber());
        }
    }
}

Recommended Answers

All 3 Replies

Those two "Read" classes sound like they should just be methods. Read... could return a Transaction object, and getResults could take a Transaction as a parameter.

I guess that's one way. I was thinking of modulating two tasks (even though both tasks are to read two different files) and definitely making things more complicated then they should be.

thanks for pointing that out James.

How do I close this post?

... I haven't seen the spec so I don't know the requirements, but maybe you should be thinking along the lines of a Transaction class, a class to represent the data file (with its read method that returns a new transaction), and an application class that ties eveything together. If getResults just displays the results for one Transaction it should be an instance method in the Transaction class.

Anyway.. you;ll find a "Mark Question Solved" button at the bottom of this page.
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.