- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.println("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.println(this.name); System.out.println(this.age); } public static void main(String args[]){ FinalMethods obj = new FinalMethods(); obj.display(); } }
Output
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.println("Name: "+obj.name); System.out.println("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.
Example
public class Student { String name; int age; { name = "Krishna"; age = 25; } public static void main(String args[]){ Student std = new Student(); System.out.println(std.age); System.out.println(std.name); } }
Output
25 Krishna
- Related Articles
- How many ways are there to initialize a final variable in java?
- Class with a constructor to initialize instance variables in Java
- How many ways can get the instance of a Class class in Java?
- What are class variables, instance variables and local variables in Java?
- How many ways are there to register a driver in Java?
- How many ways are there to convert an Array to ArrayList in Java?
- What is the difference between class variables and instance variables in Java?
- Instance variables in Java
- How many variables are there in the following linear equation ?$3x = 3+x$
- How many types of constructors are there in Java?
- 3 ways to initialize an object in Java
- How to initialize variables in C#?
- How many ways to iterate a TreeSet in Java?
- How many ways to iterate a LinkedList in Java?
- Are there any ways to Manipulate Strings in Java.
