1

My book says that one of the crucial differences between local variables and instance variables are that we must initialize local variables and instance variables get a default value of 0 if you don't initialize them. Shortly before this I did an example introducing constructors, and what public void and public double and return do.

Consider the following example which I just did in my book. Here balance is an instance variable.We have two constructors below it. If an instance variable gets a default value of zero, why do I need the first constructor

public Account(){
        balance = 0;    
    }

saying that if I call something like Account acc = new Account(); balance = 0. Doesn't it get to zero automatically? At least thats my understanding from my book

Heres the full code i was working on

public class Account {
    private double balance;
    public Account(){
        balance = 0;    
    }
    public Account(double initialBalance){

        balance = initialBalance;
    }
    public void deposit(double amount){
        balance = balance+amount;
    }

    public void withdraw(double amount){
        balance = balance-amount;
    }

    public double getBalance(){
        return balance;
    }   
}



public class Test {
public static void main(String [] args){
    Account acc = new Account(500);
    acc.deposit(500);
    System.out.println(acc.getBalance());
}   
}
2
  • 1
    Why do you think you need a constructor with balance = 0? Commented Jul 19, 2016 at 13:04
  • To set the balance to zero, if I dont put any parameters. But of course later in the book it said "instance variables get a default value of 0", so I was just wondering what the point of that was given that line is true. Commented Jul 19, 2016 at 13:08

4 Answers 4

6

You don't need the first constructor as you never call it anywhere.

Assuming it was called somewhere, you wouldn't need the line balance = 0 in it, but some people would still add it, just to make it explicitly visible and clear that it's intentional (sometimes automatic things are used unintentionally).

If you removed the constructor entirely and tried to instantiate an Account using just new Account() (without any parameters), then that wouldn't work as you don't have a zero-argument constructor anymore (another magic thing: Java will generate one for you if and only if you don't have any other constructors).

Sign up to request clarification or add additional context in comments.

6 Comments

So what does my book mean when it says "instance variables get a default value of 0"
@cresjoy the variable would have been set to 0 without that line balance = 0 as well. But as Joachim pointed out, it is added to make it clear that the value is zero upon creating the object. It is not required, but it doesn't hurt either ;-)
ah so the constructor NEEDS to exist, but balance =0, doesent.
No, if all class variables can be initialized automaticly you dont even NEED a standard cunstructor. The compiler will add it for you, just like it automaticly initializes the variables. In your case, you could delete the whole cunstructor and you will still be able to call it.
It doesent let me do that says constructor isn't defined
|
0

Yes, it does have 0.0 as a default value.

All primitive types (int, double, ...) will be initialized to 0 and all reference to other types (somehow extending Object) will be initialized to null.

It is good practise to (re)initialize the members to 0 (or any other value that make sense for the class), to show you didn't forget the variable. When you later add new members, if its value is not initialized in a constructor, it can be a hint that you forgot about it.

You can also decide to initialize it to any value during declaration:

private double balance = 0.0;

and do the same for all other members. In that case, they would receive that value when the constructor is not assigning any specific. That pattern could be used to show your intent of having those values as default, and the ones set by a constructor as an "override".

Comments

0

Yes, all primitive types (int, double, long...) are automaticly initialized with 0. Like my previous speaker said it is a good practise to add the line, to makes things clearer and cleaner.

But think also about complex types (String, Integer, Double, or your own Class). They will be initialized with null. If you try to access this default value you will get a NullPointerException, so a existing default value would be better.

Comments

0

A constructor is used to initialize the instance variables.The default value is 0(after initialization in case of int). However,in your code you are not even calling the default constructor.So,in your code you can remove the default constructor. Moreover,even if you call the default constructor ,the values will be initialized automatically.You don't even have to mention balance=0 in your code.Only this would be enough:

public account(){}

However,if you don't call any constructor and try to access the instance variables through a reference variable you will get the same error you get in case of local variable -
The variable might not have been initialised

You can learn better from here https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.