Static

Are reference variables deferenced?

We all know that the static keyword is used to refer the members of the class by using the classname rather than the instance variable. Thats why they are static members are called as class-level members.

What if you access the static members through the instance variables?

Well, there is no such hard and fast rules that you should access the static members only through the class. But its very well suggested so as to save the purpose of them! The compiler may very well warn you saying that "the static member <member-name> should be accessed in a static way"!

See the below piece of code.

Test t1= null;
System.out.println(t1.total);

Here the instance variable t1 is not actually dereferenced. So the above piece of code will never throw a NullPointerException.

Static Vs Non-Static Members

Questions

  • Can static method access non-static variables from subclass/any other class?
  • Can non-static method access static variables/final static variables from subclass/any other class?

The answers are:

  • Yes, they may but indirectly.
  • Yes, very well.

A sample program to explain the same is here.

/* ================================================
  My own program for just making use in wikidot.com
 ================================================*/
 
class BaseClass
{
    static int staticInt = 1;
    int nonStaticInt = 2;
 
    public static void staticMethodBase()
    {
        System.out.println("staticMethodBase - staticInt="+staticInt);
        //Notice how it is accessed! - has to be accessed via an object!
        System.out.println("staticMethodBase - nonStaticInt="+new BaseClass().nonStaticInt);
    }
    public void nonStaticMethodBase()
    {
        System.out.println("nonStaticMethodBase - staticInt="+staticInt);
        System.out.println("nonStaticMethodBase - nonStaticInt="+nonStaticInt);
    }
}
class SubClass extends BaseClass
{
    public static void staticMethodBase()
    {
        System.out.println("staticMethodSub - staticInt="+staticInt);
        //same as in baseclass. the object can be of either base or subclass!
        System.out.println("staticMethodSub - nonStaticInt="+new BaseClass().nonStaticInt);
    }
    public void nonStaticMethodBase()
    {
        System.out.println("staticMethodSub - staticInt="+staticInt);
        System.out.println("staticMethodSub - nonStaticInt="+nonStaticInt);
    }
 
}
public class StaticNonStaticTest
{
    public static void main(String[] args)
    {
        System.out.println("--- Calling Base Class ---");
        callBase();
        System.out.println("--- Calling Sub Class ---");
        callSub();
 
    }
    public static void callBase()
    {
        BaseClass.staticMethodBase();
        new BaseClass().nonStaticMethodBase();
    }
    public static void callSub()
    {
        SubClass.staticMethodBase();
        // this is an example that you CANNOT call the non-static method directly!
        new SubClass().nonStaticMethodBase();
    }
}

The output it produces is as follows:

--- Calling Base Class ---
staticMethodBase - staticInt=1
staticMethodBase - nonStaticInt=2
nonStaticMethodBase - staticInt=1
nonStaticMethodBase - nonStaticInt=2
--- Calling Sub Class ---
staticMethodSub - staticInt=1
staticMethodSub - nonStaticInt=2
staticMethodSub - staticInt=1
staticMethodSub - nonStaticInt=2
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.