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

static block and static variable?

 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static int a=10;
static
{
int a=10;
}
---
can anyone please explain what is the differenct between the static block and static variable. regarding scope, static block is accssible only in the same class whereas static variable can be accessed from any class.. this is what my prog says.. am i right?? let me know other differences please
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"can anyone please explain what is the differenct between the static block and static variable"

That is not a simple question to answer, as there is little comparison to made between them. You might ask:
"what is the difference between a local variable and a finally block?"

As simple as I can make:
A static variable stores a value that is shared between all instances (or the non-instance) of the Class it is defined in.

A static block is a section of code that gets executed when Class is first loaded.

"regarding scope, static block is accssible only in the same class"

This makes no sense to me. Blocks (including static blocks) don't have accessibility. For example, you can't call a static block, from inside the class or from another class. The static block is executed by the JVM when the class is loaded, that's all.

" whereas static variable can be accessed from any class"

Static variables follow the same accessability rules as any other variable. They can be private, protected, public, or have default visibility. So this statement by itself is true only if the static variable is declared public.
 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve Luke, i have understood a part of what you have told.
The following code(which has a static variable) compiles and gives output as 10.

but when i replace class one, with a static block it gives me an err.
i think the answer for this will solve my issues.. can you please help
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An example of a static variable vs a non-static variable:



Notice that the value of static variable x will keep increasing because it belongs to the 'class'. Everytime you call 'new', it will call the var x.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has to do with scope of variables in general. A variable's scope only lasts as long as the enclosing block ( { and } brackets). So in your first example, you define a variable that is a static member of the one class by putting it inside the class' block:

As long as the one class can be referenced, so to can the var inside it.

In the second example you create a new block (happens to be a static block) inside of the class block and declare the variable inside this new block. Once that block closes the variable is lost:

The only time you can reference var in this second case is from within the same block (or any inner block). You can't access it from anywhere else in the same class or not.

The same would be true for non-static variables and and any non-static blocks as well (including constructors, methods, try {} catch {} finally {}, etc...)*.


*for initializer statement is a special exception to the enclosing {} brackets defining scope.

Originally posted by venkatesh badrinathan:
Thanks Steve Luke, i have understood a part of what you have told.
The following code(which has a static variable) compiles and gives output as 10.

but when i replace class one, with a static block it gives me an err.
i think the answer for this will solve my issues.. can you please help

[ August 08, 2008: Message edited by: Steve Luke ]
 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you steve..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic