• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static variable

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test {
private static Test test = new Test();
private static String name; // private static String name = "abc";

private Test() {
name = name + "xyz";
System.out.println("name = " + name);
}

...
} // end of class

Question --

1. when the code executes 1st line, it jumps to the constructor to continue, but inside the constructor, how does the code know "name", I mean, the program only knows that "name" has been defined when it executes the 2nd line, right ? But it hasn't gone to 2nd line yet (it jumps from 1st line to the constructor). Why isn't it a problem ??

2. if I replace the 2nd line by the commented out syntax, why does it still prints "xyz" ? This means the initial value of "name" is not taken ? I guess it is because of the order of the static values. But if that's the reason, then why it doesn't complain for the the first scenario ??
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello There are two stages. One is compilation where most of the variable names are resolved and the execution of statements will come into picture at runtime. Hence there is no problem I suppose
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To understand why it works like you saw, you have to get acquinted with initializators. Also, you have to be aware there is a difference between declaration and definition.

Let's start with the latter. Declaration is when you tell the compiler what variables (or fields) you want to use. Example for this is

Definition is when the space is allocated for the object.

There is one small problem with the above: these are terms in the C language, and while it works well there, it does not really apply for Java; I am pretty sure, that even in the first case, at least the space for the reference is allocated. Also there may be a problem with primitives and the String class. However, I will use these terms in this post.

Now for the initializators. Besides constructors, Java also has a place to put initializator code to: they are called initializators. There are two types of them: static and non-static. The example class below has one of both (also a contructor).

The static initializator runs when the class is loaded into memory. The non-static initializator runs when the class is instantiated, just before the constructor.

There is only one static and one non-static initializator for a class. You can write more, but the compiler will just group them. Also it does not matter where you write them in your class; the commands will run according to the last paragraph. The order of the commands will be based on their order in the file, however. So,

will be changed to

compile time.

Now, we are almost there! One last thing you have to know, is that the compiler also separates declaration from definition. So, even if you write

, it will be changed to


If you put the above together, I guess you see now what happens in your program. So, the answers to your questions are:

1. Declarations are always "known" before ANY instructions (including definitions). So of course name is known to your program.

2. You know now that in the initializator, instructions are placed in the order they are written in the source. So here, test = new Test() runs first (calling the constructor!), then name = "abc";. If you swap the two lines, "abcxyz" will be written. But keep in mind this scenario is a bit tricky, as your static initializator instantiates the class in the first line, thereby running the non-static initializator and calling the constructor before going to the second line of the static ini.
(Also, it writes "nullxyz", because a null String is written as "null" and not "")
[ November 18, 2006: Message edited by: David Nemeskey ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

I want to add only one point.
You cannot say that the non-static initialization block does not run before the constructor runs.

It runs after the constructors call to super, but before the rest of the constructor runs.

eg
prints:
A constructor
non-static init Block in B
B constructor





Yours,
Bu.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would it be possible for someone to post the full code so that I can compile this and play arround with it.

Best wishes
Dianne
 
Dianne Gerrelli
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry just getting back into Java, I forgot that the file name needs to be the same as the class. I now get the expected output.

Best wishes
Dianne :roll:
 
David Nemeskey
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Burkhard: thanks for pointing that out! I forgot it (and forgot about inheritance as well)
 
Hareesh Ram Chanchali
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Thanks for your detailed explanation. David special thanks to you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic