GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 487 of 492    Print  
static data member
what is the use of static data member?


  
Total Answers and Comments: 4 Last Update: August 04, 2008     Asked by: amitjhjsr 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
July 12, 2008 22:52:08   #1  
r.praveenkumar Member Since: October 2007   Contribution: 24    

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 | No
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 | No
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 | No
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 | No


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape