Static Modifier in Java:
In java we can have a Static Block, Static Variable, Static Method. Static modifier specifies that a variable or method is the same for all objects of a particular class. Some times we need a common variable or method for all objects of a part icular class. Typically, new variables are allocated for each instance of a class.
Variable declared as being static is only allocated once, regardless of how many objects are instantiated and all instantiated objects share the same instance of the static variable. Similarly, a static method is one whose implementation is exactly the same for all objects of a part icular class. Static methods have access only to static variables.
Example of a static member variable and a static method:
static int refCount;
static int getRefCount() {
return refCount;
}
A beneficial side effect of static members is that they can be accessed without having to create an instance of a class. Static method or a variable is not attached to a particular object, but rather to the class as a whole. They are allocated when the class is loaded.
A static initializer block resembles a method with no name, no arguments, and no return type. It doesn't need a name, because there is no need to refer to it from outside the class definition. The code in a static initializer block is executed by the virtual machine when the class is loaded.
Example of a Static Block
static{
date1 = new Date();
for(int count = 0; count <>
var = var+1;
}
Static blocks are blocks defined within the body of a class using the static keyword but which are not inside any other blocks.
Popular Posts
-
______________________________________________________________ Posted by Super Hot Figure Read More... [Source: Super Hot Figure - Posted...
-
Anora Semiconductor Labs Pvt Ltd Subject: Looking for Engineers in Silicon Validation Position: Product Development Enginee...
-
Builds custom Facebook fan pages for large and small businesses. Need to buy Facebook fans or Facebook likes Buy Cheap and Real Fac...
-
Job Portal: Conflate Solutions - Java/J2EE Developer
-
Mahindra Satyam Off Campus Recruitment for 2009 & 2010 passouts | BE/Btech & MCA hiring | Last Date 10 March 2011 Campus Drive...
-
Location: Hyderabad Eligibility: • Qualification - B.E, B.Tech, M.C.A and M.Sc in any stream with First class. • Should have knowledge i...
-
FYI Capgemini will be conducting Open Campus Recuritment Programme for 2009 and 2010 passout students in Hyderabad on 24th Februa...
-
Job Position : Credit Underwriter Experience : 0 – 2 Years Qualification : M.A,MBA/PGDM Job Location : Chennai Job Responsibilities : * A...
-
Método Dinheiro O artigo detalha a seguir o meu top 10 pessoal métodos de ganhar dinheiro com a Internet. Método Dinheiro ...
-
test politics
Powered by Blogger.
0 comments: