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());
}
}
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).
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".
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.
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
Related
I have a quick question. I know how to use the keyword this in Java with a constructor that has parameters/arguments. Can you use this with a default constructor that has no parameters/arguments?
Example with code Class BankAccount below.
We created a method within this class to withdraw as much as possible. Within the method, I created a new BankAccount object to test with the tests provided by the Professor. Instead of creating theaccount object, he wanted me to use this. Is this possible without a constructor that holds parameters/arguments?
public double getOrAsMuchAsPossible(double requestAmount) throws InvalidAmountException, InsufficientFundsException
{
//Declare and initialize the variable amount to be used with in the method
double amount = 0;
//Create a new BankAccount object account
BankAccount account = new BankAccount();
//Deposit money into the account
account.deposit(400);
//Try to get requestAmount
try
{
//Set the amount to the request amount and withdraw from account
amount = requestAmount;
account.withdraw(requestAmount);
}
//Catch the exception with the InsufficientFundsException
catch(InsufficientFundsException exception)
{
System.out.println("Withdrawing amount: " + amount + " that is larger than balance: " + balance + " is not allowed");
}
//If the account balance is less than the amount requested
if(account.balance<requestAmount)
{
//The amount will equal the account balance, withdraw the amount from the account
amount = account.getBalance();
account.withdraw(amount);
}
return amount;
}
The java keyword "this" has no special interaction with constructors. It is often used in constructors to distinguish between parameter names and the newly created object's fields.
Something like
public class BankAccount {
private int accountNum;
public BankAccount() {
this.accountNum = 4;
}
}
Is perfectly valid, but redundant.
The main value to the "this" keyword in java is to access a field in a higher scope that has been masked in the current scope.
Classic Setter example
public void setAccountNum(int accountNum) {
this.accountNum = accountNum;
}
In this case all references to accountNum would refer to the parameter. Use of the "this" keyword allows us to specify that it is the object's field called accountNum that is to be assigned a value.
Firstly, what is "this" keyword in java?
From oracle java docs:
Using the this Keyword within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
So coming to your question, your constructor need not to be parameterized to use this. You can use this keyword with default constructor as well.
Basically you only need to remember "The this keyword refers to the current object in a method or constructor."
This question already has answers here:
Default values and initialization in Java
(11 answers)
Closed 6 years ago.
I understand that, Constructors are used to initialize the instance variables.
Default constructor will create by compiler itself, if we did not create the same.
If we are creating the parameterized constructor then compiler won't create the default constructor.
I have written a code, to ignore the instance variable to be handled by constructor. But it got initialize without constructor. How it come possible to initialize variable without the constructor?
Please find the code snippet below for better understanding
public class ClassWithoutDefault {
int number;
String name;
//Intailizing name variable alone by using parameterized constructor
ClassWithoutDefault(String name){
this.name = name
}
void show(){
System.out.println("Name is"+name+"Number is"+number );
}
}
//Main class
public class ConstructorTest {
public static void main(String[] args) {
ClassWithoutDefault classWithoutDefault = new ClassWithoutDefault("Hari");
classWithoutDefault.show();
}
}
Output
Name is Hari
Number is 0
How the variable number got initialized as 0, without the constructor?. Could any one please help me to understand this?
Each member variable is assigned a default value (when an instance of a class is created), and if you don't assign it anything else, it retains that default value. The default value of primitive numeric types is 0.
Whenever a new object of a class is created, :
Strings are initialized by null.
Numbers are initialized by 0 (integers), or 0.0(floating-point).
booleans are initialized by false.
chars are initialized by \u0000
Arrays are initialized by the default values of their components.
Other Objects are initialized by null.
Hence, your number was initialized by 0.
When you create an object,first compiler copy that class file to memory and create the template of the object in the memory. And then it creates a copy according to that template and add it to your reference variable.
If you declared a static variable then that variable is created with the template,thats why static variables have the last updated value everytime. Because for all objects there is only one common variable in that name in the memory.
But when you create instance variable it is made with every object.
So when an object is created everytime, it is initialized to its Default Value.
//byte 0
//short 0
//int 0
//long 0
//float 0.0
//double 0.0
//char null
//boolean false
//Reference Type, class type - null
I was wondering what the difference is between
public final type attribute_name;
and
private type attribute_name;
public type getA_name() {
return attribute_name;
}
Basically I want to make an attribute read-only, so it can't change after it has been initialized.
Do I make it public final, or do I make it private, and only make it accesible through a get method (without a set method)?
When it's not final but private, the class itself is able to change the value.
A final field MUST be set before the constructor exits. Once set, the reference cannot be modified (the value cannot be reassigned). Emphasis on the cannot be reassigned. This means that while the reference cannot change, the value itself can change.
This is legal:
final List<Integer> list = new List<Integer>();
list.add(5); // the value of list changes, but the reference doesn't
This is not:
final List<Integer> list = new List<Integer>();
list = new List<Integer>(); // may seem sort of redundant but the compiler won't allow it nonetheless
A private variable with a only getter can be reassigned internally by the class that holds it (but it's not visible externally so it cannot be reassigned outside the class holding it). Also, outside the class the reference is inaccessible so the variable cannot be modified except by the class holding it.
A final variable cannot be reassigned anywhere, but if it's public, another class can still access the reference and change the value of whatever object it points to.
If you don't want the variable to be reassigned after initialization as you described, use both final and private.
Use final for something like this:
public class User {
private final long registrationTimeMillis;
public User(/* various parameters probably would be here */) {
registrationTimeMillis = System.currentTimeMillis();
}
public long getRegistrationTimeMillis() {
return registrationTimeMillis;
}
}
We don't expect that a user's registration time will change, so it makes sense to not allow it to change after construction.
Use private with no setter for something like this:
public class VendingController() {
private int drinksStocked = 0;
private int drinksDispensed = 0;
public void dispenseDrink() {
drinksDispensed++;
}
public void stockDrinks(int numberOfDrinks) {
drinksStocked = getDrinksRemaining() + numberOfDrinks;
drinksDispensed = 0;
}
public int getDrinksRemaining() {
return drinksStocked - drinksDispensed;
}
}
We don't want the value of drinksDispensed to change except when dispenseDrink() or stockDrinks(int numberOfDrinks) is called. It still needs to be able to be reassigned by it's own class when the vending machine is refilled though, so we shouldn't make it final
With respect to using public final, generally in Java that's only done for constants and that static keyword is also included since constants shouldn't be dependent on an instance.
An example of when it makes sense to use public static final
public class UnitConversions {
public static final double CENTIMETERS_PER_INCH = 2.54;
}
It could then be used in a method as follows
public double convertFromCentimetersToInches(double centimeters) {
return centimeters / UnitConversions.CENTIMETERS_PER_INCH;
}
Best of luck OP and happy coding.
More reading on final fields
This depends on some factors.
If this is a real constant that is known before and will never change, then use final. In Java final fields can be initialized in the constructor as well, so if your value is known at construction time then you can use final too.
If this value gets set (once, multiple times) during runtime then use private + getter.
The final modifier allows a field to be assigned only once - it cannot be changed after that and it has to be set at during object construction (that is, before the constructor returns).
If you want to make the field read-only, use the principles of information hiding: make it private and provide a public getter that returns the field (or a copy of it for non-primitive types).
You should use public final only for true constants. Even if your field is immutable because of final it is often a good idea to still make it private.
The correct way is to think in the future. What would help you achieve your goals? Maybe later you would also like to give that variable a value. If I were you, I'd do this by creatin a get method and keeping the variable private.
Full documentation for final keyword : http://en.wikipedia.org/wiki/Final_(Java)
Depends on where you want to access it from. Public variables can be accessed from any class within the project and package where private can only be accessed from the class where the variable is.
The 'final' operator makes it permanent and read-only.
Let's assume that type is a reference to an object, not a primitive type.
public final type attribute_name means that attribute_name cannot be reassigned to refer to something else. But attribute_name can be used to call a method that changes its state.
In private type attribute_name, only methods within the class can call methods on attribute_name.
So if you want it to remain constant, use approach (2). Limit the public methods to ones that ultimately call methods on attribute_name that don't modify its state.
I have noticed a thing that a constructor and a simple method of a class do the same work. what is the exact reason to create a construct of a class? If i create MyClass(){} constructor and MyClassMethod(){} method it will do the same work as I write the body part of those method and constructor. So what is the need of construct? Does it have any special use ?
A constructor and a method are two different things. The fact that you can write the same or similar code inside them is irrelevant.
When a new object is created a constructor is called. If you don't specify one the compiler will create a default one for you. This is the place where initializaton of the object's fields takes place and memory is allocated for the object. This is a concept that all object-oriented languages have. A new object must be initialized somehow. Memory needs to be allocated. In Java you don't manage the memory yourself (at least not directly anyway) so this is the purpose of the constructor. Note that since a constructor is always executed, this behaviour is enforced as soon as you call e.g. Person p = new Person();.
Now since a constructor is always being called, you have an option here: do you let the default constructor execute or do you create one yourself? Perhaps there are fields that need to be initialized in a way other than their default values. Or perhaps you need to not allow creating an object without providing some values. If you define a constructor yourself, the compiler does not create a default one for you. So if I have public Person(String firstName, String lastName) {} then I have created a specific rule that is again enforced by the system: a new object of class Person cannot be created unless you give a first name and last name:
Person p = new Person(); // this would give a compile error
Person p = new Person("John", "Smith"); // this is the only way to create an object now
Using a method you cannot enforce this. The programmer using your class might call your method or not. The constructor is a part of the lifecycle of the object. Methods define the behaviour of the object
Some points :
1) Constructors are the only way to set final instance variables .
public class SomeType {
final int x ;
SomeType(int y){
x=y;
}
}
2) A class with private constructor cannot be sub classed.
3) If your class is a subclass and the base class doesn't have a default constructor , then you need a constructor in your class to call the super class constructor.
One of the benefits of using a constructor over a method is that you can be assured the constructor was called and the work within the constructor was performed.
The language specifies that to construct an object a constructor must be called. So if you use a custom method to establish the initial state of your object, you will need to call the default constructor first. Why make two method calls when you can perform the work in one call the constructor and be assured the object has been properly initialized?
public class Test(){
private Integer x;
public Test(){
}
public Test(Integer x){
this.x = x;
}
public void setX(Integer x){
this.x = x;
}
public void doSomethingWithX(){
this.x.toString();
}
}
Test test = new Test(8);
test.doSomethingWithX(); //I know x has been declared and assigned
Test test = new Test();
test.doSomethingWithX(); //X has not been assigned results in NPE
If you create a new Object of MyClass it will automatically call the constructor - you can initialize all members within it, and be sure that this object´s members are all initialized.
Generally:
A constructor is always called once when you create a new Object of this class, and you can´t call it manually.
And don´t do "real" work in a constructor, as it will slow down the creation of objects of this class - only initialize your class/members there.
You can also use different constructors, depending on your needs - but if you create a constructor, there is no more default constructor!
Example:
public MyClass {
int score;
public MyClass(int sc) { // already know the score
score = sc;
}
public MyClass() { // don´t know the score yet
score = 1;
}
public void addScore() {
score += 5; // i know for sure that score is not zero
}
}
Essentially a constructor is just a special method that implicitly returns an object of its containing type. You should generally use constructors for creating objects - this is what people expect to see.
However, there is a useful idiom called the factory method (more info at this link) which is essentially using a static method to construct an object, the key advantages being
You can give a factory method a more descriptive name (whereas of course a standard constructor has to be named after the containing class).
They don't have to return an object, giving more flexibility.
They can return a sub-types of the class.
You can set final fields without initializer in a constructor. This helps to build immutable instances:
class Number extends Expr {
private final int n;
public Number(int n) {
this.n = n;
}
public int getValue() {
return this.n;
}
}
So after a constructor like this, you can rely on the fact that the instance is initialized completely (and in this case, it's values are immutable/constant).
Constructor is not like simple methods. It is called every time when the object of that particular class is created. You don't need to call it explicitly.
There are somethings that we need to do immediately when the object is created, for instance when you create a GUI kind of thing you want to set many properties on the time of creation like size of window etc.
Another benefit of constructor is security of class. You cannot create a object unless you know the right perimeters of constructor.
More details:http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
A constructor is a special method of a class or structure in object-oriented programming that initializes an object of that type.
Some points :
1. A constructor eliminates placing the default values.
2. A constructor eliminates calling the normal method implicitly.
These are the benefits of constructors.
Automatic initialization of objects at the time of their declaration.
Multiple ways to initialize objects according to the number of
arguments passes while declaration.
The objects of child class can be initialised by the constructors of base class.
I'm working on homework and I won't post the full code but I'm stuck on something that's probably simple and I can't find it in my book so I need to be pointed in the right direction.
I'm working with classes and interfaces.
Basically in my main code I have a line like this
CheckingAccount checking = new CheckingAccount(1.0); // $1 monthly fee
I was told to create a class called CheckingAccount and in that class I am told "This class should include an instance variable for the monthly fee that's initialized to the value that's passed to the constructor.
Since I'm new this is barely english to me and I'm assuming what that is saying is to take that 1.00 fee and declare it in the CheckingAccount class so I can create a method using that variable to calculate something.
soooo... How do I do that? I know how to create an instance variable it would be something like
public double monthly fee =
but then what? or I could be wrong. I am really doing bad at this java stuff. Any help is appreciated.
I guess another way to ask it is am I just declaring it as 1.0? or am I "importing" that value in case it changes later at some point you don't have to go through the code to change it in all of the classes?
Your requirement (as I read it) is to initialize the instance variable in the constructor, and your instantiation (new CheckingAccount(1.0);) shows you are on the right track.
What your class will need is a constructor method which receives and sets that value 1.0.
// Instance var declaration
private double monthly_fee;
// Constructor receives a double as its only param and sets the member variable
public CheckingAccount(double initial_monthly_fee) {
monthly_fee = inital_monthly_fee;
}
#Jeremy:
You're pretty much spot on (at least, your interpretation of what you've been asked to do matches my interpretation); while I don't know the actual design of the class, or whether monthly_fee needs to be public, in pseudocode you'd be looking at something like:
class CheckingAccount {
//Instance variable
double monthly_fee;
//Constructor
CheckingAccount(double monthly_fee) {
this.monthly_fee = monthly_fee;
}
//Function to multiply instance variable by some multiplier
//Arguments: Value to multiply the monthly fee by
double multiply_fee(double a_multiplier) {
return monthly_fee*a_multiplier;
}
}
You are basically right. If you haven't already, you should create a new class (it should be in it's own file called CheckingAccount) like this:
/** This is the class of Account with monthly fee. */
public class CheckingAccount {
// This is the instance variable.
// It should be 'private' for reasons you will surely learn soon.
// And NOT static, since that would be a class variable, not an instance one.
// The capitalization is called camelCase, google it up. Or, even better, find 'JavaBeans naming conventions'
private double monthlyFee;
// This is the constructor. It is called when you create the account.
// It takes one parameter, the fee, which initializes our instance variable.
// Keyword 'this' means 'this instance, this object'.
public CheckingAccount(double monthlyFee) {
this.monthlyFee = monthlyFee;
}
// Here will be your methods to calculate something...
}
Don't create an instance variable as public. It's bad practice because it violates the principle of information hiding (your teacher may call this abstraction). Instead, you can create an instance variable as
public final class CheckingAccount {
private double monthlyFee;
// The rest of the class goes here
public double getMonthlyFee() { // This method is called an accessor for monthlyFee
return monthlyFee;
}
}
Note that monthly fee isn't a valid variable name because it contains a space, and variable names can't contain spaces. Also notice that other classes access monthlyFee through a method. Because you define the method rather than making the variable public, you control access to monthlyFee a lot better (another class can't just change monthlyFee unless you define a method that makes that change).
Now to accessing monthlyFee. The method getMonthlyFee is called an accessor for a reason: it allows other classes to access that variable. So, those other classes can just call the method to get the monthly fee out of a CheckingAccount:
CheckingAccount checking = new CheckingAccount(1.0);
// A bunch of other code can go here
double fee = checking.getMonthlyFee(); // Now fee is 1.0