Results 1 to 5 of 5
  1. #1
    Java Riser's Avatar
    Java Riser is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default Using void with constructors that return no value

    Hello everyone,

    I am new to Java and new to forums as well.

    My questions is: Why can't a constructor that accepts two int values BUT RETURNS NO VALUES have the "void" modifier?

    It took me 2 days to find out that's why the program wouldn't compile:
    Java Code:
     
    
    public [COLOR="Red"]void[/COLOR] Rectangle (int w, int h) {
    
    if (w > 0 && w < 30 && h > 0 && h < 30) {
    
       width=w;
       height=h; 
       System.out.println("A rectangle was created with width="+width+"and height="+height); }
    
    else{ System.out.println("Width must be from 1-29");
    
    }
    }
    This will NOT compile, but without the "void", it will. "cannot find symbol", it said. It puzzled me for 2 horrible days because since it does not return a value, it should accept it. I didn't need it to be void but still it was very hard to figure out the problem. Does anyone know why it won't accept it? I'm fine now, but I want to learn why it thins it's returning a value, is it the println?

    Thanks!

    *Edited to add "[/code]"
    Last edited by Java Riser; 02-17-2010 at 12:16 AM.

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

    Default

    If you give it a return type, be it void or anything, it is not a constructor but a method because that is how constructors and methods are defined.

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    That is correct, constructors can't have a void return type, because a constructors job is to return an Object instance of its class.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by m00nchile View Post
    That is correct, constructors can't have a void return type, because a constructors job is to return an Object instance of its class.
    Strictly speaking that is not true: it's the 'new' operator that returns an object of a certain class; the constructor only fills in the fields of the new object and does what it has to do.

    kind regards,

    Jos

  5. #5
    Java Riser's Avatar
    Java Riser is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    My thanks to everyone. I see! Wish the course had explained that!

    Thread closed!

Similar Threads

  1. Java Native Access (JNA) return types of void *
    By burnumd in forum Advanced Java
    Replies: 5
    Last Post: 01-15-2010, 01:09 AM
  2. 'void' type not allowed here
    By Torgero in forum New To Java
    Replies: 15
    Last Post: 10-05-2008, 12:08 AM
  3. Replies: 5
    Last Post: 01-25-2008, 12:53 AM
  4. is void a type?
    By mary in forum New To Java
    Replies: 3
    Last Post: 08-01-2007, 09:12 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
  •