1

I am trying to build a class with a constructor, mutators and accessors. Reading through books and online, I am made to learn that you can call a constructor with or without parameters. However, my case below seems not to work. I am not even able to compile without errors. It works when I use student jane = new student(""). What am I doing wrong?

Devolution.java:6: cannot find symbol
symbol  : constructor student()
location: class Governor
        student jane = new student();
                         ^
public class designers {

   public static void main(String[] args) { 

    student jane = new student();
    student smith = new student("jane", "36365355", "Nairobi", "Male");

    System.out.println("Janes's Properties: "+jane.name() + " " + jane.nationalID() + " " + jane.county()+"  "+jane.gender());
    System.out.println("Smith's Properties: "+smith.name() + " " + smith.nationalID() + " " + smith.county()+"  "+smith.gender());

   }    
 } 

other code is below

public class student {

   //Private fields
   private String name;
   private String nationalID;
   private String county;
   private String gender;   

   //Constructor method
   public student(String name, String nationalID, String county, String gender)
   {
     this.name = name;
     this.nationalID = nationalID;
     this.county = county;
     this.gender = gender;

   }

   //Accessor for name
   public String name()
   {
     return name;
   }

   //Accessor for nationalID
   public String nationalID()
   {
     return nationalID;
   }

   //Accessor for county
   public String county()
   {
     return county;
   }

    //Accessor for gender
   public String gender()
   {
     return gender;
   }

 }
4
  • 2
    Just a note, class names should start with uppercase letters in Java. Commented Oct 23, 2014 at 18:15
  • When formatting code in a question, either use the code sample button or indent the code 4 spaces. The snippets button should only be used for js, html and/or css. Commented Oct 23, 2014 at 18:17
  • Thanks @JamesMontagne. Very Helpful indeed. There is so much overwhelming info. Commented Oct 23, 2014 at 18:29
  • Many thanks guys everyone. I think I love Java. There is so much knowledge out there, so helpful and to the point! Commented Oct 23, 2014 at 18:35

6 Answers 6

6

A constructor is a way of creating an instance of a class:

Student s = new Student(...);

will create a new instance of the Student class and enable you to access it using s.

Often, when you create an instance of a class, you need to specify certain information that's used in building the instance. In the case of a student, that might be the name, the age, and so on. You'd have a constructor that looks like this:

public Student(String name, int age) {
    //...
}

But in some contexts, you can build an instance of a class without needing (at least initially) to specify anything. So you might, for instance, have a constructor like this

public Student() {
    //...
}

which leaves the name and age fields blank or zeroed out, until you later set them with another method of the class.

The critical point for what you're doing is that you've made a constructor that requires various parameters, but you haven't specified one like this second example that doesn't require any. As things stand, you can write

Student s = new Student("Bob", "ABC12345", "Surrey", "Male");

because you've got a constructor that takes four Strings as arguments. But you can't write

Student s = new Student();

because you didn't create a constructor that takes no arguments.

The slight wrinkle in this is that if you don't specify any constructors in your class, then Java will automatically create one for you that takes no arguments and doesn't do anything special. So if you don't write any constructors, you'll get one for free that looks like this:

public Student() {
}

But that's only if you don't write any of your own. Since you've specified one that does take parameters, Java won't give you a no-argument one for free. You have to put it in yourself if you want to be able to create instances without any arguments.

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

2 Comments

Many many thanks @chiastic-security. Very incisive. You just made me get the concept.
Technically the "free" default constructor looks like public class Point { public Point() { super(); } } (docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.9)
3

You've only written one constructor - the one with four parameters. You don't have a constructor without parameters, so you can't write new student().

Note that if you don't write any constructors at all, the compiler will automatically make a constructor for you, without parameters, but as soon as you write one constructor, this doesn't happen.

By the way, most people use capital letters for class names (so Student, not student). This makes it easy to distinguish them from the names of other identifiers. It would be good for you to get into the habit of doing the same.

Comments

3

You don't have a constuctor without parameters in the student class. Such a constructor is generated by the compiler only if you haven't defined any other constructors, which you have.

Just add the constructor :

   public student()
   {
       this.name = null;
       this.nationalID = null;
       this.county = null;
       this.gender = null;
   }

2 Comments

Wouldn't it be better to initialize it with something like "" so that it won't throw null exceptions. Won't the private variables already be null?
@JGrice The private variables would be null, but it doesn't hurt to write this explicitly. Makes the code more readable in my opinion.
1

You need to make another constructor as follow:

public Student(){
//do things here
}

Explanation:

When no constructors are defined in a class then there is a default constructor(without any parameters) already. In which case you don't need to define it. But if you have any constructor with some parameters, then you need to define the constructor without parameters as well.

13 Comments

Explanation is blank, which is a clear indication of FGITW behavior.
@Nabin I didn't downvote, just commented when I read it without the explanation. People generally try to submit half a post if they are striving to be the fastest, instead of trying to write a perfect post.
You haven't done anything wrong, @Nabin. Just ignore the downvoters.
Personally, I'm very careful not to click on a button before finishing my
@Nabin - someone (not me) believes it's worthy of a downvote. That person's opinion is just as valid as anyone else's. We don't need 10 people to all come along and cancel out one person's opinion.
|
1

Its called overloading the constructor. In your class, declare a constructor again without parameter requirements. See this post for more info

Comments

0

You don't have a constructor without parameters. That would only be the case when you had not write an own one. When you want to have the possibility to make an object of the class with or without parameters, you need two different constructors in your code.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.