Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Puzzle 5: Static Variables and Object Instantiation

DZone's Guide to

Java Puzzle 5: Static Variables and Object Instantiation

Catch up with the answer to the last Java Quiz about passing parameters to constructors and dive into a more open-ended puzzle about static variables and instantiation.

· Java Zone ·
Free Resource

Automist automates your software deliver experience. It's how modern teams deliver modern software.

Before we start with this week's quiz, here is the answer to Java Quiz 4: Passing a Parameter to a Constructor

  1. The statement new MyClass(5).method(2) creates a new object by passing the one-argument constructor of the class MyClass.

  2. By passing 5 to the constructor, the statement y += i; increments the value of y by 5. So y = 3 + 5 = 8.

  3. By invoking the method and passing the value 2 to it, the statement y+= i; increments the value of y by 2. So, y = 8 + 2 = 10.

  4. The statement new MyClass(new MyClass(5).method(2), 4); creates a new object by calling the two-argument constructor.

  5. The method returns the value 10. So, the statement new MyClass(new MyClass(5).method(2), 4); is equivalent to new MyClass(10, 4);

  6. The y += (i + i2); increments the value of y by 10 + 4 = 3 + 10 + 4 = 17.
    The correct answer is: d.

Onto the Puzzle!

Usually we offer a Java quiz, but today, as shown from the title, we have a Java puzzle. Let's start it!

If the following code is compiled and run, it writes nothing to the standard output.

Write only one statement at line 29. The statement should apply the following.

  1. The statement creates the object mc from the class MyClass.

  2. The statement should assign the value y to the object str, assign the value 9 to the variable i, and the value 8 to the variable i2.

  3. As a result of adding the statement, the output of the code should become y98.

What is that statement?

public class MyClass
{
    static String str = "x";
    static int i = 2;
    static int i2;
   
    MyClass(String str, int i, int i2)
    {
        MyClass.i2 = i2;
        if(!str.equals("") || i != 0 || i2 != 3)
        {
            System.out.print(i2);
        }
    }
    String strMethod(String str)
    {
        MyClass.str = str;
        System.out.print(str);
        return str;
    }
    int intMethod(int i)
    {
        MyClass.i = i;
        System.out.print(i);
        return i;
    }
    public static void main(String[] args)
    {
        // add your statement here!
    }
}


The correct answer and its explanation will be included in the next quiz in two weeks! For more Java quizzes, puzzles, and assignments, take a look at my site!

Get the open source Atomist Software Delivery Machine and start automating your delivery right there on your own laptop, today!

Topics:
java ,quiz ,puzzle ,parameter passing ,object instantiation

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}