Java doubts not able to interpret

Hi

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.

Thanks
Kelly


kelly_jesiccaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

imladrisCommented:
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;
        return;
    }
}

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;

    static
    {   eax=3;
        return;
    }

    StaticEx(int v)
    {   ea=v;
        return;
    }
}

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.
0
imladrisCommented:
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.
0
imladrisCommented:
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;
        return(1);
    }

    static init2()
    {   s=2;
        return(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.

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

imladrisCommented:
Sorry, as it stands getVal will return 2. If you reversed the declaration (and initialization) of a1 and a2, getVal would return 1.
0
kelly_jesiccaAuthor Commented:
Hi imladris,

Thanx forclearing my doubts, Could you tell me why does the order of declaration change the output for static blocks?
0
imladrisCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
imladrisCommented:
Did this help?

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

If not, perhaps a clarifying question would be helpful.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.