user2918160
user2918160

Reputation: 103

Can you call a private constructor from another class?

So I am trying to write a method that writes a list of students put into a treemap, but my treemap is in my StudentViewcontroller. Am I able to call it from my into my write student method.

  Here is my WriteStudent method so far:
  Public void WriteStudent(PrintWriter pw) {
   try{
      for (Map.Entry<String, Student> s : studentMap.entrySet()){
      }
     }catch(FileNotFoundException e){
  }
  }

Here is my treemap for the studentviewcontroller:

  public class StudentViewController {
    //A tree map to store the students
    private TreeMap<String, Student> studentMap = new TreeMap<String, Student>();

Upvotes: 1

Views: 528

Answers (2)

you cannot invoke a private constructor from another class, private constructors, methods and variables can be invoked only by it's owner class.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

If you had a private constructor then you couldn't invoke it directly from any other class. However, you don't have a private constructor. Your class has a field (with private access permissions) which is of type TreeMap<String,Student> and named studentMap. This is a private constructor.

private StudentViewController() {
  super();
}

Upvotes: 1

Related Questions