Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Java doubts not able to interpret

Posted on 2005-03-29
Medium Priority
Last Modified: 2010-03-31

I am new to Java programming and I am learning it with some online tutorials. I have a few questions

Is Instance Initialization and Initialization Block the same, similarly what are static blocks. Could you give me an example that illustrates these both.

I watn to replace a constructor with an Initiaialization block Is it possible to do so and which initialization block do I have to use and what is the basic difference between them.

I don’t understand the difference between class initialization and class instantiation. I tried reading it but could not understand what they mean… It would be better if I could have some examples that show the differences and where class instantiation and class initialization occur.

Why do we  need class initialization and instantiation? And what are the steps taken during initialization and instantiation

Can we produce different outputs where the only difference is in the order of declarations of static variables  in java. Can you show me an example and how do we do that and why does that happen.


Question by:kelly_jesicca
  • 6
LVL 16

Expert Comment

ID: 13655633
Instance initialization is what happens when a new object is constructed.
A static block is a set of statements that is executed when an object is first loaded.

Class Ex
{   int ea;

    Ex(int v)
    {   ea=v;

When you create an Ex object with new:

Ex e=new Ex(5);

then a new Ex object is created, and the instance variable ea is set to 5. If you create another Ex object

Ex f=new Ex(6);

you will have another Ex object (referenced by f) in which the instance variable ea is set to 6.

Class StaticEx
{   static int eax;
    int ea;

    {   eax=3;

    StaticEx(int v)
    {   ea=v;

In this case there is a static initialization block that initializes the static variable eax. This will be done when the class is first loaded. A static variable is accessible for *every* instance of the class. So if you do:

StaticEx e=new StaticEx(5);
StaticEx f=new StaticEx(6);

you will have two instances of a StaticEx class. One will have the instance variable ea set to 5, the other to 6. However, there is only *one* static member eax, which was set to 3 when it was first loaded. *Both* instances of StaticEx reference the one static member eax.

An Initialization block is something that does exist in the Java language. However it is only used with anonymous classes. I would strongly recommend that, as a beginner, you not get caught up in trying to deal with this at this stage.

Class initialization and class instantiation describe very simiilar things. Class instantiation is the whole process that is involved in creating a new object. Class initialization is the piece of the instantiation process that concerns itself with initializing instance variables. So in the course of instantiating a class (making a new object), many things are done (allocating memory, calling the class loader etc. etc.), and also (at some point in the process) initializing the variables (which is class initialization).

No, it is not possible to get different results based on the order of declarations of static variables.
LVL 16

Expert Comment

ID: 13655664
Well, on this one:

>No, it is not possible to get different results based on the order of declarations of static variables.

It is possible there is some obscure effect that I am not aware of. As I'm not sure what is motivating this particular question, I probably shouldn't categorically say that it could never happen.

However, I have not (in more than a decade of Java programming) run into any such thing, nor can I imagine any situation in which it would be desirable.
LVL 16

Expert Comment

ID: 13656361
You *could*, of course, get differences in the programs output, based on the order of *initialization* of static variable (which may be related to the order of their declaration).

In the following case:

class StaticSample
{   static int s;
     static int a1=init1();
     static int a2=init2();
     static int a3=s;

     int getVal()
     {   return(a3);

    static int init1()
    {   s=1;

    static init2()
    {   s=2;

The value returned by getVal will, in a sense, depend on the order of declaration of a1 and a2. As it stands getVal will return 1. If you reversed the declaration (and initialization) of a1 and a2, getVal would return 2.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 16

Expert Comment

ID: 13656379
Sorry, as it stands getVal will return 2. If you reversed the declaration (and initialization) of a1 and a2, getVal would return 1.

Author Comment

ID: 13659494
Hi imladris,

Thanx forclearing my doubts, Could you tell me why does the order of declaration change the output for static blocks?
LVL 16

Accepted Solution

imladris earned 2000 total points
ID: 13663145
Well, as I said, I don't see how the order of *declaration* can change "output".

The order of *initialization* can, of course, change things, as per the example above.

A static block is just a block of static code that initializes things (as per the further back above example of StaticEx). So, again, I don't see how the order of *declaration* could change "output".
Also, again, assuming it is possible, it is certainly not desirable.
LVL 16

Expert Comment

ID: 13682017
Did this help?

If so, it is now time to grade the answer.

If not, perhaps a clarifying question would be helpful.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question