Over a million developers have joined DZone.

Java Quiz: Static Members in Java

Learn about how static members in Java behave in DZone's newest Java quiz!

· Java Zone

Learn more about the advantages of moving from a monolithic to microservices architecture.  Brought to you in partnership with IBM.

Last Week's Answer

The correct answer is the code writes "[d, a, b, e]" to the standard output.

This Week's Quiz


  1. To demonstrate some behaviors and tricks of static members in Java

  2. Mind training to understand what happens next.

What Happens When the Following Program is Compiled and Run?

public class MyClass {

    static int x;
    StringBuffer sb = new StringBuffer();
    static StringBuffer sb2 = new StringBuffer();

    public MyClass() {
    public void method(){
        x += 3;
    public void method2(){
        x += 3;
    public static void main(String[] args){
        MyClass mc = new MyClass();
        MyClass mc2 = new MyClass();
        MyClass mc3 = new MyClass();
        System.out.println(mc2.sb + "-" + mc2.sb2);

Let us know your thoughts in the comments, and visit Sar's site here!

From Idea to Application gives you the architecture to quickly build, manage and run a range of applications (web, mobile, big data, new smart devices, etc.) on an open-standard, cloud-based platform. See why developers are using IBM Bluemix. Brought to you in partnership with IBM.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}