Anurag Singh
Anurag Singh

Reputation: 727

getting default values of variables during run time in java

When i run this program in Eclipse, I get the default values of the variables and not the values which i enter during runtime. When I run the program, I get the default values of the variables which has been assigned in the constructor public account(), and not those values which I am entering during the runtime. Could someone please explain the reason why this is happening.

Here is my code...

public class bank {
    public static void main(String[] args){
        account[] obj=new account[3]; 

        for(int i=0;i<3;i++){
            obj[i]=new account();
            obj[i].entry();
            obj[i].display();
        }

        for(int i=0;i<3;i++){
            obj[i]=new account();
            String res=obj[i].getCustomer_name();
            JOptionPane.showMessageDialog(null, res);
        }

        account obj=new account();
        obj.entry();
        obj.display();
    }

}

class account {
    private String customer_name;
    private int acc_num;
    private double open_balance;

    public account() {
        customer_name="ADAM";
        acc_num=001;
        open_balance=100;
    }

    public void entry() {
        String customer_name=JOptionPane.showInputDialog(null, "Enter the customer name");
        String acc_num=JOptionPane.showInputDialog(null, "Enterthe account number");
        String open_balance=JOptionPane.showInputDialog(null, "Enter the balance");

        int acc_num1=Integer.parseInt(acc_num);
        double open_balance1=Double.parseDouble(open_balance);
    }

    public void display() {
        JOptionPane.showMessageDialog(null,customer_name);
    }

    public String getCustomer_name() {
        return customer_name;
    }
}

Upvotes: 0

Views: 1200

Answers (2)

trutheality
trutheality

Reputation: 23465

The problem is shadowing: When you declare a variable inside a method with the same name as a member variable, it shadows the member variable.

E.g.

class MyClass{
    int var = 12;

    void shadows(){
        int var = 5;
        System.out.println(var); // prints 5
        System.out.println(this.var); // prints 12
    }

    void noshadows(){
        System.out.println(var); // prints 12
    }
}

Your fix:

public void entry()
{
    customer_name=JOptionPane.showInputDialog(null, "Enter the customer name");
    String acc_num_str=JOptionPane.showInputDialog(null, "Enterthe account number");

    String open_balance_str=JOptionPane.showInputDialog(null, "Enter the balance");

    acc_num=Integer.parseInt(acc_num_str);

    open_balance=Double.parseDouble(open_balance_str);
}

Upvotes: 2

David
David

Reputation: 218827

I'm no Java expert, so this may just be a guess, but this code here looks incorrect:

public account()
{
    customer_name="ADAM";
    acc_num=001;
    open_balance=100;
}

public void entry()
{
    String customer_name=JOptionPane.showInputDialog(null, "Enter the customer name");
    String acc_num=JOptionPane.showInputDialog(null, "Enterthe account number");

    String open_balance=JOptionPane.showInputDialog(null, "Enter the balance");

    int acc_num1=Integer.parseInt(acc_num);

    double open_balance1=Double.parseDouble(open_balance);

}

In the entry() method you're re-declaring the customer_name, acc_num, and open_balance variables. So when you reference them within that method you're referencing the newly-created local variables, not the class-level variables.

I'm surprised it even compiled, but I guess that just shows what I don't know about Java. Were there no compiler warnings at all?

Upvotes: 0

Related Questions