Results 1 to 8 of 8
  1. #1
    baig-sh is offline Member
    Join Date
    Nov 2011
    Posts
    35
    Rep Power
    0

    Post Constructor Error!

    Hi all,
    i m totally new with java please help me.
    here is my 2 file's code:
    -------------------------------------
    public class Student {

    private String name;
    private int rollNo;

    private static int countStudents = 0;

    public void setName(String name){

    this.name= name;
    }

    public void setRollNo(int rollNo) {
    if(rollNo>0) {
    this.rollNo = rollNo;
    }
    else {
    this.rollNo = 100;
    }
    }

    public String getName() {
    return name;
    }

    public int getRollNo() {
    return rollNo;
    }

    public static int getCountStudents() {

    return countStudents;
    }

    public Student(){

    name = "not set";
    rollNo = 100;
    countStudents += 1;
    }


    public Student(String name, int rollNo) {
    setName(name);
    setRollNo(rollNo);
    countStudents += 1;
    }

    public Student(Student s) {
    name = s.name;
    rollNo = s.rollNo;
    countStudents += 1;
    }

    public void print() {

    System.out.print("Student Name: " +name);
    System.out.println("RollNo: " + rollNo);
    }
    public String toString() {

    return "Name: " +name + "RollNO: " +rollNo;
    }

    public void finalize() {
    countStudents -= 1;
    }
    }
    ----------------------------------------

    public class finalTest {

    public static void main(String args[]) {
    int numObjects;

    numObjects = Student.getCountStudents();
    System.out.println("Student Objects: " +numObjects);

    Student s1 = new Student("Mike: " +15);
    System.out.println("Student Objects: " +s1.toString());

    numObjects = Student.getCountStudents();
    System.out.println("Student Objects: " +numObjects);

    Student s2 = new Student("Smith: ", 13);
    System.out.println("student Objects: " +s2); // calling 2 string

    numObjects = Student.getCountStudents();
    System.out.println("Student Objects: " +numObjects);

    s1 = null;

    System.gc();

    numObjects = Student.getCountStudents();
    System.out.println("Student Objects: " +numObjects);

    } // End of main
    } // End of class

    Student.java compiled successfully but when i try to compile finalTest.java
    then i facing an error:

    D:\tutor\exer>javac finalTest.java
    finalTest.java:9: cannot find symbol
    symbol : constructor Student(java.lang.String)
    location: class Student
    Student s1 = new Student("Mike: " +15);
    ^
    1 error
    please Help me. Thanks
    Last edited by baig-sh; 11-02-2011 at 11:18 PM.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Constructor Error!

    Maybe you have an typing error, but there is no constructor in your Student class which takes only one String!
    Student s1 = new Student("Mike: " +15);

    "Mike: "+15 = one String

    Perhaps you mean Student s1 = new Student("Mike: " , 15); ??

  3. #3
    baig-sh is offline Member
    Join Date
    Nov 2011
    Posts
    35
    Rep Power
    0

    Default Re: Constructor Error!

    finalTest.java:9: cannot find symbol
    symbol : constructor Student(java.lang.String)
    location: class Student
    Student s1 = new Student("Mike: " +15);
    ^
    1 error

    --------------------Error indicates about this Constructor-----

    public Student(Student s) {
    name = s.name;
    rollNo = s.rollNo;
    countStudents += 1;
    }
    constructor at his place but compiler generate error.
    * See in red color.
    Last edited by baig-sh; 11-02-2011 at 11:19 PM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Constructor Error!

    What was wrong with the reply posted by eRaaaa? It explains exactly what your problem is.

  5. #5
    baig-sh is offline Member
    Join Date
    Nov 2011
    Posts
    35
    Rep Power
    0

    Default Re: Constructor Error!

    I checked again and again ..no typing or spell mistake. but i can't understand the error.
    anyone has solution?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: Constructor Error!

    Quote Originally Posted by baig-sh View Post
    I checked again and again ..no typing or spell mistake. but i can't understand the error.
    anyone has solution?
    eRaaaa already gave you the solution: you don't have a constructor that takes a single String parameter.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Constructor Error!

    Java Code:
    Student s1 = new Student("Mike: " +15);
    Student s2 = new Student("Smith: ", 13);
    Even a blind person can see the difference between those two lines of code.

  8. #8
    baig-sh is offline Member
    Join Date
    Nov 2011
    Posts
    35
    Rep Power
    0

    Default Re: Constructor Error!

    Java Code:
    1
    2

    Student s1 = new Student("Mike: " +15);
    Student s2 = new Student("Smith: ", 13);
    Even a blind person can see the difference between those two lines of code.
    Thank u Junky for great help its amazing!! problem solved. Thanks!

Similar Threads

  1. help fixing error in constructor
    By katiebear128 in forum New To Java
    Replies: 3
    Last Post: 10-13-2011, 03:40 AM
  2. Constructor Error
    By collwill in forum New To Java
    Replies: 1
    Last Post: 03-20-2011, 08:16 PM
  3. constructor error in image manipulation app
    By bmcnamara in forum New To Java
    Replies: 3
    Last Post: 12-05-2009, 08:06 PM
  4. Constructor Error ?
    By sysout in forum New To Java
    Replies: 4
    Last Post: 08-25-2009, 05:39 AM
  5. why do I got this error about the constructor ?
    By aneuryzma in forum New To Java
    Replies: 4
    Last Post: 08-14-2008, 10:22 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •