• 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

Question on static variable

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a public static variable in my class set to null.
I assign it a value in the constructor of its class.
What will be returned when i use this variable directly in method of another class. Will it return null or the value assigned?
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It can be either one. If you only assigned the public static variable inside the constructor of the class, then it depend whether you have ever instantiate an instance of the object and passed in the value to constructor. If you have ever do it, then it will return the value. If not, then you'll get null.

to illustrate


I'm just curious, your signature shows you got SCJP with 90% score. Then how come you do not understand how static work?
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you only assigned the public static variable inside the constructor of the class, then it depend whether you have ever instantiate an instance of the object and passed in the value to constructor. If you have ever do it, then it will return the value. If not, then you'll get null.



Can you explain the above sentence in little simple terms please.
I am totally confused about you ares saying.

 
nitin pai
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm just curious, your signature shows you got SCJP with 90% score. Then how come you do not understand how static work?




Your curiosity is justified.
I gave SCJP somewhere around march 2006. Later on, I never got involved in using core java. I mean I had to use all framework syntaxes which has made my core java quite rusty. So I am trying to get back to the basics.

I was sure of the reason behind my question but just wanted to confirm. Hope you don't mind
 
Wirianto Djunaidi
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the long winded explaination, because I was addressing to the context of Nitin's question.

Basically when you declare a static, it means the variable or method belong to the class not to an instance of a class (commonly known as object). For example:

In the above class definition we have 2 variables: myVariable and objVariable. The code above just a definition, so the real thing that happen during runtime when you have instances of the above class where you can have by instantiating them using new keyword, i.e:


With the above example, objVariable is belong to myObj while you can say that myVariable belong to MyClass.class (or Class<MyClass> in generic style syntax)

The class instance (MyClass.class) automatically loaded to existance when it is referred anywhere in your code by the JVM, along with it all the static members. Usually static variables are initialized in two ways, direct or through static block. For example:


But in Nitin's case, he tied the initialization of the static variable with the instantiation of an object.

Because the static belong to the class, not to the object. It is accesible at any time whether an object of that class has ever instantiated or not. That is why the answer to Nitin's question is it depend. If anywhere in his code he ever instantiates an object of that class, then he would have inialized the static variable and will get it when using it. But if he has never instantiates an object of that class, then the static variable would have not been initialized which is by default is null.
 
nitin pai
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Wirianto, that was a really nice explanation. I am sure it will clear some confusions for those giving SCJP.

Well I have knowledge about what you explained. But can you also mention something on static constructors? I have just heard about them but never read.

I actually wanted to implement a scenario in which i have a public static hashmap in my class and I wanted it to be initialized with about 10 key,value pairs before accessing it directly through another class. Thats where my question arose which I had asked. I have still not got the solution for this.

Currently what I am doing is, made a private hashmap and initialized it in constructor and am accessing it with a getter method.
 
Wirianto Djunaidi
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Technically there is no such thing as static constructor. But there is something that called static block, which is just a regular code block declared static. You typically see them in the code look like this:


Static block is block of code that will automatically executed when the Classloader finish loading the class. So this is somewhere you can usually put your code to initialize your static variables where the initialization can be long, such as populating Collection object.

Standard static rules apply, it can only call static method and manipulate static variables or local variable in the static block.

TIP: You are not limited to 1 static block. You can have multiple static block inside your file. I assume the Classloader will just execute them sequencially as defined in the file. So just be aware on dependency of your variables initialization if you spread them out into multiple static blocks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic