Pages

data member qun ans

Q) What does static mean?

static - stationary, standing, not moving
Q) What is static data member?
·         Static data member is a data member declared with a static data modifier.
·         Static data members are created inside class memory (static memory).
·         Static data members is not created for every object it is created only once in class memory and shared by all objects.
Q) What are the types of static members?
       Java support four types of static members

1.    static variables
2.    static blocks
3.    static methods
4.    main method
Q) What is the execution order of all static variables?

Ans: static variable is a class level variable which is declared by using static keyword. All the static variables are executed by jvm in the order they are defined from top to botttom.

Note: All static variables are having individual memory locations.

Q) How to access static data member?
·           In three ways we can access static data members
1.    Classname.datamember
2.    Reference.datamember( here reference can point null)
3.    Reference.datamember( here reference can point object)
Q) What is non - static data member?
·         Non Static data member is a data member declared without a static data modifier.
·         Non Static data members are created inside object memory
·         Non static data members are created for every object.

Q) What are the types of Non Static members?

Java supports four types of non static members
1.    Non static variables
2.    Non static blocks
3.    Non static methods
4.    constructors
Q) When do all these members get memory locations and by whom?
All non static data members get memory location only if object is created with new keyword and constructor of that class. JVM will not provide memory location for these members by default by itself.

Q) How to access Non static data member?

·           In two ways we can access non static data members
1.    Reference.datamember( here reference must point object, if reference points null then NullPointerException is raised)
2.    new ClassName().datamember

Q) How can we call non static members from other non static members?
We can call non static members from other non static members directly by their name.
Ex:
class MCA
          {
               int rno=100;
               float avg=78.7f;
               void first()
                      {
                         System.out.println("I am in first");
                     }
          void second()
                      {
                   System.out.println(rno);
              System.out.println(avg);          
                   first();

                     }
          public static void main(String[] args) {
                   MCA m=new MCA();
                   m.second();
          }
          }


Q) Why we cannot call Non static members from static members directly?

Because static member can be accessed directly without object creation, hence compiler doesn’t allow non static members directly from static members.