2

How can I have the Edit() method be called by default when I construct a Breuk object?

public class Breuk {
    public Breuk() {//constructor

    }

    private static Breuk Edit (Breuk b){ //function 

    }
}
4
  • Did you mean private static Breuk Edit(Breuk b){?
    – Tachyon
    Mar 4, 2015 at 19:53
  • yes, sorry thats what i meant
    – omaros88
    Mar 4, 2015 at 19:54
  • 2
    Calling a method from a constructor is just like calling a method from anywhere else. One caution though: If you are going to call an instance method from a constructor, it's a smart idea to make the instance method final. Mar 4, 2015 at 19:55
  • This smells a lot like an XY problem. Why do you need the method to be called by default? Mar 5, 2015 at 0:39

3 Answers 3

1

Every time a new Breuk object is created, the Edit() method is called by default since it is placed inside the constructor. As the method is static, it has to be called in a statically way, i.e. ClassName.staticMethod(). As the method expects a Breuk object as argument, you pass this reference to it, meaning the object that's being constructed.

By the way, all method names should be lowercase according to Java conventions. So, consider renaming Edit(...) to edit(...).

class Breuk {
  int x;
  int y;

  public Breuk(int x, int y) {
    Breuk.Edit(this);
  }

  private static void Edit(Breuk b){
    //edits breuk
  }
}
1
  • Please also provide an explanation for why this does what the OP wants. It adds value for anyone coming along later and not understanding, whether or not the OP does.
    – Nic
    Mar 4, 2015 at 20:23
1

First of all, you should stick to Java naming conventions, which state that method names should be lowerCamelCase. So you should rename your Edit() method to edit().

If you want the edit() method to be called by default when constructing an instance of the Breuk class, you can use an initializer block.

Excerpt from the Java Tutorial:

The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.

For your example, you could try something like this:

public class Breuk {

    { // initializer block
        Breuk.edit(this); // always called by default, 
                          // no matter which constructor is used
    }

    public Breuk() { // no-args constructor

    }

    public Breuk(int a1, int a2) { // another constructor

    }

    private static Breuk edit(Breuk b) { // function 

    }
}

Here I'm assuming you want to pass the instance being constructed to the edit() method, that's why this is passed as an argument.

EDIT:

In case this assumption is true, I suggest you make the edit() method return void, as you can't assign an instance to this, since it's final (and it doesn't make any sense, either).

3
  • I'm not sure this answers the original question. I think this answers a question one level more complex. It also uses fairly unusual Java syntax - I've never seen that done in real life, and I'm not sure I want to. Mar 5, 2015 at 0:38
  • @AshleyFrieze I put the stress on "by default", I admit it. For me, "by default" means "automatically", i.e. without making the invocation within the constructor.
    – fps
    Mar 5, 2015 at 0:51
  • What you have commented as a default constructor is actually just a no-arguments constructor. The default constructor is the one that's there if you don't explicitly define one (which also happens to be a no-arguments constructor).
    – herman
    Mar 5, 2015 at 2:16
0

Just call the method from the end of your default constructor. There's no need to think about any "default" calling here. Just implement it.

public class Breuk {
    public Breuk() {
        // constructor
        edit(this);
    }

    private static Breuk edit (Breuk b){ 
        // class-level processing on any Breuk that is constructed 

    }
}
3
  • How do you know that "there's no need to think about any default calling"? The OP is actually asking "the Edit() method be called by default when I construct a Breuk object".
    – fps
    Mar 5, 2015 at 0:49
  • From the example given, there's only one constructor. When that's the case, the simple answer is what I've suggested. If he'd said "from all my constructors" then I'd initially be looking at making the constructors chain each other using the this(...) syntax and then, if that didn't work, I'd consider duplicating the call to edit rather than the initialisation block thing, which would be harder to debug than spelling it out. Mar 5, 2015 at 0:51
  • Ok, as someone has suggested in the comments, this appears to be an XY problem. Maybe we should ask the OP to clarify. Regarding initializer blocks, it's a basic concept covered in the official Oracle's Java Tutorial (not in some advanced newsletter). Anyways, we're kind of guessing what the OP might have wanted to ask us, so this discussion leads nowhere. Your point is absolutely valid, and mine's too.
    – fps
    Mar 5, 2015 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.