Results 1 to 6 of 6
Like Tree3Likes
  • 2 Post By Tolls
  • 1 Post By awinston

Thread: Can't find symbol. Constructor

  1. #1
    Noob. is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Exclamation Can't find symbol. Constructor

    Hello pros :)
    I'm trying to implement a simple program which uses a constructor.
    Here's my code:

    Java Code:
    import java.io.*;
    
    class add
    {
    	private int c;
    	
    	void add(int a, int b)
    	{
    		c = a + b;
    	}	
    	
    	void display()
    	{
    		System.out.println("Result is " +c);
    	}
    }
    
    class Mainn
    {
    	public static void main(String args[]) throws IOException
    	{
    		int x, y;
    		BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
    		System.out.println("Enter 2 numbers: ");
    		x = Integer.parseInt(obj.readLine());
    		y = Integer.parseInt(obj.readLine());
    
    		add o = new add(x, y);
    		o.display();
    	}
    }
    Here' my error:add_cons.java:28: cannot find symbol
    symbol : constructor add(int,int)
    location: class add
    add o = new add(x, y);
    ^
    1 error

    I'm using Ubuntu. Java 1.7

    Thanks,
    Sid

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default Re: Can't find symbol. Constructor

    A constructor doesn't have a return value in its signature...that includes 'void'.
    so not:
    void add(int x, int y) {etc.}
    but
    add(int x, int y) {etc.}
    awinston and Noob. like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Can't find symbol. Constructor

    You don't have a constructor with 2 int arguments. I'm assuming that you think this is a constructor:

    Quote Originally Posted by Noob. View Post
    Java Code:
    	void add(int a, int b)
    	{
    		c = a + b;
    	}
    ...but that is actually just a method. A definition of a constructor uses the syntax: public className(type arg0, type arg1, type arg2, ....)

    If I wasn't clear enough, check out this tutorial: Constructors
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Can't find symbol. Constructor

    Not fast enough...
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  5. #5
    Noob. is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Re: Can't find symbol. Constructor

    Oops. Not even void?! That was pretty noob
    Thanks

  6. #6
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Can't find symbol. Constructor

    Quote Originally Posted by Noob. View Post
    Oops. Not even void?! That was pretty noob
    I remember reading that constructors implicitly return the object that is constructed, so explicitly stating that it returns no object would not make sense.
    Noob. likes this.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

Similar Threads

  1. cannot find symbol - constructor Gun()
    By 2341234 in forum New To Java
    Replies: 3
    Last Post: 04-02-2012, 09:48 AM
  2. overridding constructor causes can not find symbol
    By bflhr673 in forum New To Java
    Replies: 5
    Last Post: 07-27-2010, 10:36 AM
  3. cannot find symbol constructor
    By daud in forum New To Java
    Replies: 9
    Last Post: 08-13-2009, 03:53 AM
  4. Replies: 9
    Last Post: 10-18-2008, 08:26 PM
  5. Error: cannot find symbol constructor
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 08:24 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
  •