You are on page 1of 3
‘i222, 4:49 PM How many ways are thereto intalize the instance variables ofa class in java? How many ways are there to initialize the instance variables of a class in java? You can initialize the instance variables of a class using final methods, constructors or, Instance initialization blocks. Final methods Whenever you make a method final, you cannot override it. i.e. you cannot provide implementation to the superclass's final method from the subclass. i.e, The purpose of making a method final is to prevent modification of a method from outside (child class). You can also use these final methods to initialize the instance variables. Example import java.util.Scanner; public class FinalMethods { int age = getage(); String name = getName(); static Scanner sc = new Scanner(System.in) ; public static final int getage() { System.out.print1n(“Enter age value"); return sc.nextInt(); > public static final String getName() { System.out.println("Enter name value"); return sc.next(); + public void display(){ System.out.println("Name and age values: *); System. out. print1n(this.nane); System. out. println(this.age) ; + public static void main(String args[]){ Finalmethods obj = new FinalMethods(); obj .display(); + Output hitps uw ttorlspointcomvhow-nany-waye-are-there-o-inaze-thesnstance-variables.ot-a-classin-java 4 ‘i222, 4:49 PM How many ways are thereto intaize the instance variables ofa class in java? Enter age value 25 Enter name value krishna Name and age values: Krishna 25 Constructors A constructor is used to initialize an object when it is created. It is syntactically similar to a method. The difference is that the constructors have the same name as their class and, have no return type. There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation Example public class Test { String name; int age; public Test(String name, int age){ ‘this.name = name; this.age = age; + public static void main(String args[]) { Test obj = new Test("Krishna", 25); System.out.print1n("Name: "+obj.name) ; System.out.printIn("Age: "+obj.age) ; + Output Name: Krishna Age: 25 Instance initialization blocks Similar to static blocks, Java also provides instance initialization blocks which are used to initialize instance variables, as an alternative to constructors. Whenever you define an initialization block Java copies its code to the constructors. Therefore, you can also use these to share code between the constructors of a class. tps Aww ttorlspointcomvhow-nany-waye-are-here-o-iniaze-thesnstance-variabes.ota-classn-jva 28 ‘i222, 449 PM How many ways are there to Initialize the instance variables ofa cass in java? Example public class Student { String name; int age; { name = "Krishna"; age = 253 } public static void main(String args[]){ Student std = new Student(); System. out.print1n(std.age) ; System. out. print1n(std.name) ; + Output 25 krishna tps Aww ttorlspointcomvhow-nany-waye-are-here-o-iniaze-thesnstance-variabes.ota-classn-jva 313

You might also like