Static data member

What is the use of static data member?

Questions by amitjhjsr

Showing Answers 1 - 24 of 24 Answers

consider a situation where we would like to count the no of objects created for a particular class. let us consider a program

class ex
{
public int count;
public ex()
{
count=count+1;
}
public static void main(String as[])
{
ex o1,o2;
System.out.println(o1.count);
System.out.println(o2.count);
}
}
the output is
1
2

in the first line see 1 is the output though 2 objects are created this is because
o1.count is different from o2.count these two counts are totally different from each other so to maintain a data member which is common to all the objects we go for static data member.

now consider the following program which counts exactly the no of objects created.

class ex
{
public static int count;
public ex()
{
count=count+1;
}
public static void main(String as[])
{
ex o1,o2;
System.out.println(o1.count);
System.out.println(o2.count);
}
}
the output is
2
2

see now we got the correct output here the static count is common to all the objects if we change the value in one it will automatically reflect in all objects.

chanty

  • Jul 13th, 2008
 

static data member are those which do not take different memory every time when we call the  object hoe many times it may be.ex

class sample
{
static int a;
increment()
{
a++;
}
system.out.println(" amar.509@gmail.com ");
}

class mainc
{
public static void main(string args[])
{
sample s=new sample();
sample s1=new sample();
sample s2=new sample();
s.increment();
s1.increment();
s2.increment();
}
}
 
Actually in the above progarm when we cal increment() variable "a" is to be incremented for a particular object only.
But when we cal increment() with s1 and s2 the value of "a countinus to increment"

{for each object the variable which is static should not take different memory locations and it will countinue in the same memory location}

  Was this answer useful?  Yes

PROPERTYS OF static KEYWORD:

The datamembers declared with static keyword is is treated as Constants, those values cannot be changed throughout the program...

  • As well for the static data members the memory is allocate in the seprate area called as "context" area.
  • static datamemebrs are accessable without declering objects
  • they can be accessable with the class name

      eg: Student.roll_no;

  Was this answer useful?  Yes

naresh5m

  • Aug 4th, 2008
 

Static Data Member has an intersting feature for the programming languages.

1) While creating functions (2 Reduce the fode redundance) the results of that funcing will have to return to use out side of the function. If cant return that value then we dont have have a change to use the out put of the function. if we store in the static variable then it will be accessed thrught the program.

2) The properties of one class is copied for all the function of the same/another class, no need to reprasent the same thing in no. of time. Just declare it as static then it solves the probelm.

  Was this answer useful?  Yes

static members(data and methods)are contained within the class definition but exist and accessed by without creating an instance of the class.
suppose
public class StaticDemo
{
public static  double pi=3.14;
}
When the JVM loads the byte code for the class description of StaticDemo, it creates a single memory location for the pi variable and loads it with the 3.14 value.
This variable pi can be accessed without creating an instance of the class...

We can access the data directly using the name of the class, as in:

  double x = 2.0 * StaticDemo.pi;

aditya_8584

  • Mar 24th, 2009
 

Static data member has a class scope. A single copy of static data member get shared within the objects of that class. A non-static member cannot access a static member of that class.

Sarje

  • Aug 13th, 2009
 

Static data members field (variable) and method belong to class. Static fields can be used before creating an instance of the class and static method can be accessed using class name.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions