Skip to main content

Class initialization

  • Although we tend to think of initialization in terms of assigning values to variables, initialization is so much more. 
  • For example, initialization might involve opening a file and reading its contents into a memory buffer, registering a database driver, preparing a memory buffer to hold an image's contents, acquiring the resources necessary for playing a video, and so on.
  • Java supports initialization via language features collectively known as initializers.
  • Class initialization is different from Object initialization and it happens before Object initialization.


 

  • Class Initialization
    • A program contains no of classes, before the execution of java program the class loader loads the public class having the PSVM.
    • Then the byte code verifier verifies the class.
    • Then class initializes.
    • The class fields are set to default values.
    • For Example:


       

      • static boolean b;//flase
      • static byte by;//0
      • static char c;//
      • static double d;//0.0
      • static float f;//0.0
      • static int i;//0
      • static long l;//0
      • static short s;//0
      • static String st;//null


         

  • We can also explicitly assign values to the class fields.
  • Those values are assigned to the fields just after the class is loaded and before any user defined method including main executes.
  • This initialization is done using <clinit>method of the JVM. It uses machine level instructions to assign those values.
  • Although a subsequently declared class field can refer to a previously declared class field, the reverse is not true: You cannot declare a class field initializer that refers to a class field declared later in source code. In other words, Java does not permit forward references with class field initializers, as the following code fragment demonstrates:
    • static int second = 1 + first;
    • static int first = 3;
  • In the cases when you need to read the contents of the file before the main method executes then we need to use the static block.
    • static
    •    {
    •       System.out.println ("Acquiring filenames");
    •       filenames = new File (".").list ();
    •       System.out.println ("Filenames acquired");
    •    }
  • Class block initializers are useful. For example, Sun's JDBC (Java Database Connectivity) API uses class block initializers to simplify database driver registration. Consider the following code fragment:
    • Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
  • Any variable declared in the block is having the scope only upto that block.
  • JVM allows us to declare any constant field without any explicit initializer, but u have to initialize it before its first use.
    • class ClassInitializationDemo5
    • {
    •    final static double PI;
    •    static
    •    {
    •       PI = 3.14159;
    •       int i;
    •       for (i = 0; i < 5; i++)
    •            System.out.println (i);
    •    }
    •    static int j = i;//this vl cause problem as the I is local to that block
    •    public static void main (String [] args)
    •    {
    •       System.out.println ("PI = " + PI);//this vl show 3.14 as the initialization is done in static block
    •    }
    • }
  • When a class hierarchy is involved, the compiler creates a separate<clinit> method for each class in that hierarchy. At runtime, the JVM loads all hierarchy classes and calls their <clinit> methods in a top-to-bottom order. 

Popular posts from this blog

Design Patterns (TID)

The pattern is an organised way of solving some specific class of problems. These patterns come in to the picture at analysis and high-level-design phase. The first step of applying one pattern to the code base is first to understand the find the vector of change in the code base. Next step is to isolate the things that are subject to change form the things that are not. That is adding a layer of abstraction to the code. The goal of design patterns is isolating the changes in your code. Understand Inheritance and Composition as a solution to a specific class of problems. Inheritance : - It allows you to express differences in behavior (that's the thing that changes) in objects that all have the same interface (that's what stays the same). Composition : - Composition can also be considered a pattern, since it allows you to change—dynamically or statically—the objects that implement your class, and thus the way that class works. Some principles of designing the c...

Adobe Firefly

Adobe Firefly is designed to help developers create custom applications more quickly and easily. The platform is built on top of Adobe's existing Experience Cloud, which provides a range of tools for managing customer data and creating personalized experiences. One of the key features of Adobe Firefly is its ability to integrate with a wide range of other technologies and services. This means that developers can use the platform to build applications that connect with everything from social media platforms to IoT devices. Another important aspect of Adobe Firefly is its focus on speed and efficiency. The platform includes a range of pre-built components and templates that developers can use to quickly create new applications. It also includes a range of tools for testing and debugging applications, which can help to speed up the development process. Overall, Adobe Firefly looks like an exciting new platform for developers who are looking to create custom applications quickly and ef...

Primitive Obsession with Example

Primitive Obsession is the name of a code smell that occurs when we use primitive data types to represent domain ideas. For example, we use a string to represent a message or an integer to represent an amount of money. For Example: Code with Primitive Obsession // primitiveObsession.java public class primitiveObsession { public static void main ( String args []) { Integer [] cityPopulations = { 13000000 , // London 21903623 , // New York 12570000 , // Tokyo 1932763 , // Stockholm 1605602 , // Barcelona 4119190 // Sydney }; for ( Integer cityPopulation : cityPopulations ) { System . out . println ( cityPopulation ); } } } public class City { private final String name ; private final int population ; private final Continent continent ; public String getName () { return name ; } public int getPopulation () { return population ; } public Continent ge...