Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can object method call be done simultaneously with object instantiation?

I'm trying to use java class BitSet as a field for a customized class. And I want the class to use a default BitSet with all bits set.

import java.util.BitSet;

public class MyClass {
    private BitSet mask;

    public MyClass() {
        this(new BitSet(4));
        // want to set all bits first
        // something like 
        // this( new BitSet(4).set(0,3) );
    }

    public MyClass(BitSet mask) {
        this.mask = mask;
    }    
}

By default BitSet constructor unsets all bits. So before I send it as an anonymous object, I would like call set(int, int) method to set all bits. I know that I could simply initialize the field mask to a new BitSet and then call set(int, int) method from there.

However, in general I'm wondering is it possible to access an instance method at time of object instantiation?

like image 412
D Lee Avatar asked Apr 23 '16 03:04

D Lee


People also ask

What method is called just before an object is instantiated?

The constructor method is used to initialize data. It is run as soon as an object of a class is instantiated. Also known as the __init__ method, it will be the first definition of a class and looks like this: class Shark: def __init__(self): print("This is the constructor method.")

What happens when you instantiate an object?

An instance of an object can be declared by giving it a unique name that can be used in a program. This process is known as instantiation. A class can also be instantiated to create an object, a concrete instance of the class.

Can an object call a method?

Calling an object's method is similar to getting an object's variable. To call an object's method, simply append the method name to an object reference with an intervening '. ' (period), and provide any arguments to the method within enclosing parentheses.

How are object methods called in Java?

Remember that.. The dot ( . ) is used to access the object's attributes and methods. To call a method in Java, write the method name followed by a set of parentheses (), followed by a semicolon ( ; ).

Can be invoked without using any object?

Unlike instance methods, a static method is referenced by the class name and can be invoked without creating an object of class. In simpler terms, they are methods that exist even if no object has been constructed yet and that do not require an invocation object.


1 Answers

Why not writing a separate constructor that allows for the BitSet initialization? Using Java 8, this could look like something like this:

public class MyClass {
  private BitSet mask;
  public MyClass() {
    this(new BitSet(4),(m)->m.set(0,3));
  }
  public MyClass(BitSet mask,Consumer<BitSet> initializer) {
    initializer.accept(mask);
    this.mask = mask;
  }
}

You can even make that more generic by introducing a static method with type parameters:

public static <T> T initialize(T t,Consumer<T> initializer) {
  initializer.accept(t);
  return t;
} 

In that case, the earlier MyClass would look as follows:

public class MyClass {
  private BitSet mask;
  public MyClass() {
    this(initialize(new BitSet(4),(m)->m.set(0,3)));
  }
  public MyClass(BitSet mask) {
    this.mask = mask;
  }
}

UPDATE

And there is one more way, without introducing new methods or constructors:

public class MyClass {
  private BitSet mask;
  public MyClass() {
    this(new BitSet(4) {{ set(0,3); }});
  }
  public MyClass(BitSet mask) {
    this.mask = mask;
  }
}

An anonymous class is being instantiated by extending BitSet and adding an instance initialization block, hence the double curly braces.

like image 61
YoYo Avatar answered Sep 23 '22 12:09

YoYo