Amit Das
Amit Das

Reputation: 1107

Make list of non public class

I have a java file in which I have two classes in which one is public and another is default like this -

import java.util.List;

public class TaskAnswers {


    private float ratings;
    private List<Image> imageList;

    public float getRatings() {
        return ratings;
    }

    public void setRatings(float ratings) {
        this.ratings = ratings;
    }


    public List<Image> getImageList() {
        return imageList;
    }

    public void setImageList(List<Image> imageList) {
        this.imageList = imageList;
    }

}

class Image {
    private String image;
    private String caption;

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

}

I want to make a list of Image class type in another class. And when we write like this than that java file is called as what?

Upvotes: 0

Views: 70

Answers (3)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521914

Java only allows one public class per source file. Try making the Image class public and you will get compiler errors. You need to call the file TaskAnswers.java since it is a requirement that the name of the source file match the name of the public class contained in that file.

As user @NwDx mentioned the Image class is package protected, which means you will only be able to instantiate it from other Java classes that share the same package. If you need to access the Image class in a separate package, it would be a better design choice to move it into its own public class file rather than making it an inner class.

And the code example you posted in your original problem looks just fine.

Upvotes: 2

aw-think
aw-think

Reputation: 4803

If you need the class Image in other packages, you have to make it public, but in this case you need to move it to an inner class of TaskAnswers or make an own class (file).

public class TaskAnswers {


    private float ratings;
    private List<Image> imageList;

    public float getRatings() {
        return ratings;
    }

    public void setRatings(float ratings) {
        this.ratings = ratings;
    }


    public List<Image> getImageList() {
        return imageList;
    }

    public void setImageList(List<Image> imageList) {
        this.imageList = imageList;
    }



    public static class Image {
        private String image;
        private String caption;

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }

        public String getCaption() {
            return caption;
        }

        public void setCaption(String caption) {
            this.caption = caption;
        }

     }
}

For more information please look in the Java Nested Classes Tutorial

Upvotes: 1

Bhargav Modi
Bhargav Modi

Reputation: 2655

Image class is called as utility class and TaskAnswers is called as functionality implementation class you can use it in following manner and you are using the concept of high cohesion

List<Image> list = new ArrayList<Image>();

Upvotes: 0

Related Questions