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?

Pieter
  • 895
  • 11
  • 22
J. Doe
  • 19
  • 1
  • 3
    within the clone method, don't you see the = new ... ? it's exactly the same – Stultuske Dec 20 '17 at 07: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. – Smutje Dec 20 '17 at 07:01
  • 1
    Wherever you learned both sides need to be "equal" is wrong. For example https://stackoverflow.com/questions/17459553/why-do-some-people-use-the-list-base-class-to-instantiate-a-new-arraylist More correctly, both sides must be related, with the left side being a higher order object – OneCricketeer Dec 20 '17 at 07: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()`? – J. Doe Dec 20 '17 at 08:50

2 Answers2

1
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.

meditat
  • 1,197
  • 14
  • 33
1

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

Dheeraj Joshi
  • 1,522
  • 14
  • 23
  • 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()? – J. Doe Dec 20 '17 at 10:18
  • Yes,but only if that method clone returns an instance of type A – Dheeraj Joshi Dec 20 '17 at 11:16
  • according to your question in comments,clone() should return name which is an instance of class A – Dheeraj Joshi Dec 20 '17 at 11:22
  • 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? – J. Doe Dec 20 '17 at 13:33
  • Yeah it will be same,because at last you are returning the instance of class Image in both the cases – Dheeraj Joshi Dec 20 '17 at 15:40