aspose file tools*
The moose likes Beginning Java and the fly likes static block and static variable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static block and static variable?" Watch "static block and static variable?" New topic
Author

static block and static variable?

venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
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


SCJP1.5
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

"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.


Steve
venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
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

Joined: Jun 28, 2008
Posts: 99
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Aug 03, 2008
Posts: 77
Thank you steve..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static block and static variable?