GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 487 of 502    Print  
static data member
what is the use of static data member?


  
Total Answers and Comments: 8 Last Update: August 13, 2009     Asked by: amitjhjsr 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: muktasharad
 
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;

Above answer was rated as good by the following members:
pravinsharma, tanzeem khan, Mahitha Reddy.G, abhijitp, bino75
July 12, 2008 22:52:08   #1  
r.praveenkumar Member Since: October 2007   Contribution: 32    

RE: static data member
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.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 3Overall Rating: -2    
July 13, 2008 02:48:42   #2  
chanty Member Since: May 2008   Contribution: 1    

RE: static data member
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}

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 2Overall Rating: -2    
July 28, 2008 03:39:57   #3  
sudampanigrahy Member Since: July 2008   Contribution: 5    

Static data member

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;


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 2Overall Rating: -2    
August 04, 2008 10:28:21   #4  
naresh5m Member Since: July 2008   Contribution: 1    

RE: static data member
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.

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
September 05, 2008 05:37:16   #5  
muktasharad Member Since: September 2008   Contribution: 4    

RE: static data member
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;

 
Is this answer useful? Yes | NoAnswer is useful 3   Answer is not useful 0Overall Rating: +3    
November 20, 2008 08:22:19   #6  
shilpasanand Member Since: November 2008   Contribution: 5    

RE: static data member
Static Data Member is used when the user wants only a single copy of the object and that data member can only be initialized once.
 
Is this answer useful? Yes | No
March 24, 2009 00:16:01   #7  
aditya_8584 Member Since: January 2006   Contribution: 2    

RE: static data member
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.
 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
August 13, 2009 05:10:23   #8  
Sarje Member Since: August 2009   Contribution: 62    

RE: static data member
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.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape