• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

Java Initializers basics

by admin

In this post, you will learn to:

  • Describe initializers.
  • State the syntax of variable initializers.
  • State the syntax of instance initializers.

Concept of Initializers

Initializers are small pieces of code embedded in curly braces that perform initialization. Besides using constructors for initializing, initializers can be used for initializing. The different types of initializers are as follows:

  • class block initializers: The class block initializer initializes complex classes. It consists of the static keyword, an open and close brace, and the initialization code.
  • class field initializers: The class field initializer initializes class variables or class fields. It evaluates an expression with an assignment operator and assigns the result to a class field before executing any of its methods.
  • object block initializer: The object block initializer initializes complex objects. It consists of an open and close brace and the initialization code
  • object field initializers: The object field initializer initializes instance variables or object fields. It evaluates an expression with an assignment operator and assigns the result to an object field when an object is created and the constructor is called.
Note: static is a keyword in Java. A variable declared using the static keyword is a class variable and not an instance variable, such as static int age. If initializers are declared, these are invoked before constructors are invoked during object instantiation.

Object Field Initializers

Object field initializers also known as instance variable initializers are declared inside a class. An instance variable initializer contains an equal sign and an expression. The following is the syntax for initializing a field with an instance variable initializer.

Syntax:

class <classname>{
   // Object field initialization with value/expression
   <data_type> field1=<value>/<expression>
 }

Object Block Initializers

Object block initializers also known as instance initialization block are declared within the class, but outside the constructor and method definitions. An instance initializer begins with a brace bracket, followed by one or more statements and ends with a brace bracket. The following is the syntax for declaring an instance initializer within a class.

Syntax:

class <classname>{
   // Object block initialization
   {
      //Initialization code
   }
}

The following code demonstrates the working of variable initializers and instance initializers.

Code Snippet:

public class MugsBeer {
   //instance field initialization
   float price=9.50F;
   String materialUsed;
   String make;
   //instance block initialization of materialUsed,make 
   {
      make="Ogilvy";
      materialUsed="Crystal";
      System.out.println("price, materialUsed & make initialized");
   }
   
   MugsBeer() {
      System.out.println("Beer Mugs Constructor");
   }
   
   public static void main(String[] args) {
      MugsBeer x = new MugsBeer();
   }
}

Output:

price, materialUsed and make initialized
Java Classes and Objects
Java Instance Variables
Java Instance Methods
Java Initializers basics

Filed Under: Java

Some more articles you might also be interested in …

  1. Exception Handling in Java
  2. Java Methods
  3. Java Operators
  4. Introduction to Exceptions in Java
  5. Basics of Java Operators
  6. Introduction to Java Packages
  7. Java – Sending Email
  8. Java Interfaces
  9. Java if and switch-case Statements
  10. Java Modifier Types

You May Also Like

Primary Sidebar

Recent Posts

  • vgextend Command Examples in Linux
  • setpci command – configure PCI device
  • db_load command – generate db database
  • bsdtar command – Read and write tape archive files

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright