Results 1 to 3 of 3
  1. #1
    tehhparadox is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default why can't I use new?

    Hi guys

    I've just started Java and I don't know much and do not have much experience either. Anyway, I was practising, and I wrote the following script:
    Java Code:
    class printstr{
    	void printstr(String var) {
    		System.out.println("String argument: " + var);
    	}
    }
    
    class example{
    	public static void main(String args[]) {
    		printstr obj1 = new printstr("example string");
    	}
    }
    My file was named example.java

    Somehow my java compiler gave an error saying:
    Java Code:
    C:\java>javac say.java
    example.java:9: cannot find symbol
    symbol  : constructor printstr(java.lang.String)
    location: class printstr
                    printstr obj1 = new printstr("example string");
                                    ^
    1 error
    Initially I thought there was something wrong with my code. So I copied some code from the book I was learning java from which contained the keyword new, and my javac gave the same error.

    Is there something wrong with my compiler? I have the latest version of it - JDK SE 6 Update 17. Or is java2 too old or someting, because I'm learning Java2..

    Please help!
    Last edited by tehhparadox; 12-23-2009 at 07:56 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Is there something wrong with my compiler?
    Nope, it's your code. What you think is a constructor isn't because you have a "void" return type assigned to it. Constructors have no return type at all.

  3. #3
    tehhparadox is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    thanks fubarable!

    I removed 'void' and it worked fine!
    Last edited by tehhparadox; 12-23-2009 at 08:47 PM.

Posting Permissions

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