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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 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