1

I am learning Java and found this article on stackoverflow.

So there are two classes:

public class Image {

...

    public Image clone() {
        Image clone = new Image(getMagicNumber(), getHeight(), getWidth(), getMax());
        for (int i = 0; i < getHeight(); i++){

            for (int j = 0; j < getWidth(); j++){
                clone.setPixel(getPixel(i, j), i, j);
             }
        }
        return clone;
    }
}

And than there is this class:

public class Filter {

    public Filter() {

    }

    public Image linearFilter(Image image, float[][] kernel) {
        Image filtered = image.clone();

        ...

         return filtered;
    }
}

I am used to do X instancename = new X(); for creating an instance, where X is the name of the class. Are there different ways for creating an instance? For example in the Filter class: How is Image filtered = image.clone(); creating an instance? In order to create an instance I thought on both sides of the "equation" X has to be equal. What I mean by this: Image filtered = new Image();. I don't understand how Image filtered = image.clone(); is creating a new instance. Can someone explain?

4
  • 3
    within the clone method, don't you see the = new ... ? it's exactly the same Commented Dec 20, 2017 at 7:00
  • If you would have the source code for Image, you would see that Image#clone or one of its called methods would be the one to call new Image(...). Cloning an image simply hides the logic needed to create a new image while preserving the clone-semantics. Commented Dec 20, 2017 at 7:01
  • 1
    Wherever you learned both sides need to be "equal" is wrong. For example stackoverflow.com/questions/17459553/… More correctly, both sides must be related, with the left side being a higher order object Commented Dec 20, 2017 at 7:07
  • Does that mean if you have the classes A and B and within A there is a method called clone which creates an instance A name = new A(); you can just create an instance in B by doing Type newname = variable.clone()? Commented Dec 20, 2017 at 8:50

2 Answers 2

0
Image filtered = image.clone();

Is same as

Image filtered = new Image();

you can see thet clone() is a method of your class which return the instance of the class Image

But making instance using methods like clone() are supportive when you want to create only one instance of your class, you can make the instance of your class public and final, and return it using a public method.

Sign up to request clarification or add additional context in comments.

Comments

0

class: How is Image filtered = image.clone(); creating an instance?

It is creating an instance as you can see that the method clone() is returning the type Image, the first line of the method is showing this :

Image clone = new Image(getMagicNumber(), getHeight(), getWidth(), getMax());

and at last of this method clone is returned,this was the logic

I hope it helps

5 Comments

Does that mean if you have the classes A and B and within A there is a method called clone which creates an instance A name = new A(); you can just create an instance in B by doing Type newname = variable.clone()?
Yes,but only if that method clone returns an instance of type A
according to your question in comments,clone() should return name which is an instance of class A
Lest say we don't have the clone method in the class Image. But I want to do the same. Should I first create an instance in the class Filter by doing "Image filtered = new Image()"? Would this be the same?
Yeah it will be same,because at last you are returning the instance of class Image in both the cases

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.