Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the difference between static variable and static block

 
Anand Sivathanu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between static variable and static block in java
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the difference between a variable and a constructor?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or more generally, ask yourself, "What is a 'block' of code? And what would happen if I put a 'static' modifier in front of it?"
 
Anand Sivathanu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i put static for a block of code,then it will be loaded once when is class is loaded by jvm.
static variable is class variable,which will be loaded when the class is loaded.

i donot know the exact difference between these to two?
can you explain?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anand Sivathanu wrote:
i donot know the exact difference between these to two?
can you explain?


You mean to say, the order which the load?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anand Sivathanu wrote:if i put static for a block of code,then it will be loaded once when is class is loaded by jvm...

The block will do more than just load. When a class is loaded, a static block of code will execute.

Look at this code and try to predict the output. Then test it by compiling and running.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic