This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Global constant in a static block

 
Ian Lubelsky
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been working on the assignment NaturalLanguageMultiply. One of the things I can do is create a global constant if I want and I have a question about the form of a constant in a static block.



I took the example from the exercise, so as not to give anything away from the assignment. Of course it could also be my way of ensuring no unnecessary nitpicking if I goofed it somewhere.

On line 4, I write "static", but I'm not sure why. In my script I've been able to compile it and run it with and without that line. I'm wondering what purpose it serves. I tried looking the answer up on the net, but all I get is more examples of scripts written with the line "script" and how to initialise a block; but none that explains to me why I need to put the word "static" in the code.

Thanks for any info on this.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1808
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm.... I think I have used resources that don't include the second 'static,' and thus have always omitted it.

I wonder if it's style or function.....
 
d vasu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian Lubelsky wrote:
On line 4, I write "static", but I'm not sure why. In my script I've been able to compile it and run it with and without that line. I'm wondering what purpose it serves.


I see that you want to know about the significance of the "static block". The "static block" runs only once i.e; at the time when the class loads. A block without keyword static is called "instance block" and it runs every time an instance is created.

Try this : create a static block and an instance block in the same program and create two or more instances of the same class in the main() method ...... You will see the difference.

A static block runs only at the time of class loading. A static variable has only one copy. A static method can be accessed by using the class name directly.
 
Ian Lubelsky
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@d vasu. I'm not sure I follow you. I ran the script with and without the word "static" on the 4th line and I encountered no errors at all. That was the only thing I changed in the script before comiling/running it. So I'm still not sure why I need to put that 4th line in.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The blocks are initializer; they exist as instance and static initializers: JLS.

You need to look up the difference between static and non-static members and the rules for accessing them (accessing static members from a non-static context).

Do you use an IDE? If yes, you will get a warning in line 6 which should give you a hint. (Edit: You get the warning when you remove the "static" from the initializer.)
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic