arvin_codeHunk
arvin_codeHunk

Reputation: 2390

Getting null values when a class is extended by two other classes

I am working on a backend services for a financial company. This application is built using jpos,spring 3.0.5, hibernate 3.6. This application will run in multithread environment which is taken care by jpos. However I confused between which strategy i should use to define a class which properties and data-member can be accessed throughout the application.

Till now what I have done is:

public class MsgHandler 
{

    public Boolean isOffline = true;
    public Boolean isSuccess;
    public Long mobileTxnId;

    setAllData()
    {
        mobileTxnId=1000;
        ..............
    }
}

In this class basically, I used to put all necessary data which i am using frequently throughout the process. SO I extend the MsgHandler

public class A extends MsgHandler 
{

    setAllData();
    System.out.println("Mobile Txn Id: "+mobileTxnId) //its printing the values here
    B b=new B();
    b.useData();
}

Now an another class B is also extending the MsgHandler class, but in class B I am not getting the null values of mobileTxnId which is set in class A

public class B extends MsgHandler 
{
    useData()
    {
        System.out.println("Mobile Txn Id: "+mobileTxnId) //its not printing the  values here
    }
}

Why this is happening as I am extending the same class, but in class A , I am getting mobileTxnId but in class B ,I am getting null values for mobileTxnId.

is making mobileTxnId static is a good practice, even if the application is going to be used in multithread environment, please suggest me

Upvotes: 0

Views: 788

Answers (2)

darijan
darijan

Reputation: 9795

Try calling the b.setAllData() before the b.useData() or calling the setAllData() in the initialization block of the B class.

So:

public class A extends MsgHandler {
{
   setAllData();
   System.out.println("Mobile Txn Id: "+mobileTxnId) //its printing the values here
   B b=new B();
   b.setAllData(); //see what I did here
   b.useData();
}

Or:

public class B extends MsgHandler {
{
   setAllData(); //see what I did here
   System.out.println("Mobile Txn Id: "+mobileTxnId) //its not printing the  values here
}

If this is not an answer to your question, try formulating a different one. It might be that you are actually asking for something completely different (i.e. accessing the values/methods of another class that extended the same, etc)

Upvotes: 0

BackSlash
BackSlash

Reputation: 22243

a and b are not the same objects, so calling setAllData() on a object constructor doesn't make the call also in the b object constructor.

You need to call

b.setAllData()

before you use

b.useData()

Upvotes: 2

Related Questions