Best static questions in May 2012

Creating an object in a static way

53 votes

Could anyone explain how Java executes this code? I mean the order of executing each statement.

public class Foo
{
    boolean flag = sFlag;
    static Foo foo = new Foo();
    static boolean sFlag = true;

    public static void main(String[] args)
    {
        System.out.println(foo.flag);
    }
}

OUTPUT:

false

  • Class initialization starts. Initially, foo is null and sFlag is false
  • The first static variable initializer (foo) runs:
    • A new instance of Foo is created
    • The instance variable initializer for flag executes - currently sFlag is false, so the value of flag is false
  • The second static variable initializer (sFlag) executes, setting the value to true
  • Class initialization completes
  • main runs, printing out foo.flag, which is false

Note that if sFlag were declared to be final it would be treated as a compile-time constant, at which point all references to it would basically be inlined to true, so foo.flag would be true too.

Behavior of static blocks with inheritance

18 votes

I am trying to use static blocks like this:

I have a base class called Base.java

public class Base {

    static public int myVar;

}

And a derived class Derived.java:

public class Derived extends Base {

    static
    {
        Base.myVar = 10;
    }
}

My main function is like this:

public static void main(String[] args)  {
    System.out.println(Derived.myVar);
    System.out.println(Base.myVar);
}

This prints the out put as 0 0 where as I expected 10 0. Can somebody explain this behavior? Also, if I want my derived classes to set the values for a static variable how can I achieve that?

As I understand. You don't call any Derived properties (myVar belongs to Base, not to Derived). And java is not running static block from Derived. If you add some static field to Derived and access it, then java executes all static blocks.

class Base {

    static public int myVar;

}


class Derived extends Base {

    static public int myVar2;

    static
    {
        Base.myVar = 10;
    }
}


public class Main {
    public static void main( String[] args ) throws Exception {
        System.out.println(Derived.myVar2);
        System.out.println(Base.myVar);
    }
}

From java specification, when class is initialised (and static block got executed):

12.4.1 When Initialization Occurs A class or interface type T will be initialized immediately before the first occurrence of any one of the following: • T is a class and an instance of T is created.
• T is a class and a static method declared by T is invoked.
• A static field declared by T is assigned.
• A static field declared by T is used and the field is not a constant variable (§4.12.4).
• T is a top level class (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.

PHP vs ASP.NET static variables

5 votes

Static variables in ASP.NET caught me off guard today. Then i became freaked out, because this means either i have a fundamental misunderstanding of static variables in the world of the web or ASP.NET does not act like i thought it would.

So in ASP.NET

public class MyClass {
    public static bool myVar = true;    
}

If ASPUserA sets MyClass.myVar = false every other user on the system would experience these changes. So, ASPUserB would have myVar = false. My Source: http://www.foliotek.com/devblog/avoid-static-variables-in-asp-net/

class MyClassPHP {
    public static $myVar = false;
}

If PHPUserA sets MyClass::$myVar = true does this mean that every user on the system experiences these changes???

Thank you.


Upon further research i did this;

class MyClassPHP {
    public static $myVar = 0;
}

Then i had users who went to a page do this

MyClassPHP::$myVar++;
echo MyClassPHP::$myVar;

It always was 1. No matter how many times i refreshed or simultaneous connections... WOHHH that was a great conclusion, or else i am screwed!!


ASP.NET update

Upon further research and testing things i found this.

public partial class MyPage : System.Web.UI.Page
{
    public static int myInt = 0;

    protected void Page_PreInit(object sender, EventArgs e)
        myInt++;
    }

}

Then my page can display myInt.

Between the browsers (Firefox and chrome) the myInt was progressively higher as i refreshed the page. So this does not matter if your class is static. It only matters if you have static variables. They are application wide.

When running in IIS:

A static variable is not "page" specific. It is "AppDomain" specific. The only relation to the "page" would be the path (so to speak) of the variable (MyProject.MyPage.MyVariable for example). Because all users of your application are running in the same AppDomain (i.e. same IIS application folder), then they will all use the same static variable. So... eventually your users are going to see each each other's information since they are all sharing that one single static variable.

PHP however tracks statics PER USER INSTANCE, so I guess you could call them "safer from the singleton dangerzone".