java - Can I make a variable final after it has been declared? -


i'm making banking model, , account class has accountnumber field. account number should never change, cannot set field final because prevent constructor setting it.

if it's not possible this, doesn't matter. it's cs assignment want make sure i'm doing best way can.

would best implementation make field , setter method private?

the constructor can set if marked final e.g. following legal:

public class bankaccount {      private final int accountnumber;      public bankaccount(int accountnumber) {         this.accountnumber = accountnumber;     }  } 

in fact if field marked final not initialised in declaration must set in constructors.

if not put public setter on class account number can't changed outside class marking final prevent (accidentally) being changed methods inside class.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -