ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

staticData

Updated on July 26, 2012

Static Keyword:--

static keyword is used to define class Members.

class Members can be of following types.

1.static data member

2.static block

3.static methods

static data members represents attribute of a class,only a single copy of static data member is created at the time of class loading.this single copy of the class can be shared to all the objects of the class.

static data members are used to store common information of of objects.

Single copy of static data members is shared in class

class AB
{
int a;
static int b;
public static void main(String args[])
{
AB x =new AB();
AB y=new AB();
}
}

Here x and y are reference variable ,and "a" is instance member and "b" is class member .

in this program b is available for both x and y reference variable using instance variable "a" .

for the execution of java application memory is divided in three parts(for now), generally it has five parts but here i will cover only three types of momory on behalf of our program.

1.stack:- Local variables of methods are stored in stack.

2.heap:- objects are created in Heap memory block.

3.class area: static data members are stored in class area

so here in last example x and y are in stack memory block,'a' will be in heap memory block and 'b' will be in class area memory block.

static block

A static block is a block of statement, which is implicitly executed when a class is loaded.It is basically used to initialize static data members of a class.

Syntax:

static

{

statements

}

Ψ: static methods represents behavior of the class. they are invoke using class Name.

Syntax:---

1. From within the class:--

methodName(args if any);

2. From a Different class:---

className.methodName(Args if any);


Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article