Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

void

 
anuj thite
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all members,

I am new to java programming.

i know that constructor can't return any value not even void.

what does it mean ???
Can you tell me the difference between "void" and no return value.

Please help.

Thanks in advance.
 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

you cannot represent 'void' it is just a keyword in the java language

a method with void in its declartion does not return a 'void' it returns nothing
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All methods must have a return type. I believe that "void" is just to allow the compiler know that a particular method will have a return type of nothing aka will not return any data. A constructor on the other hand is not a method, even though they are similar... therefor you can't declare one as having a return type, including void. The compiler already knows what a constructor does so it does not need void added.

That's my understanding on it anyways.
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Legg wrote:All methods must have a return type. I believe that "void" is just to allow the compiler know that a particular method will have a return type of nothing aka will not return any data. A constructor on the other hand is not a method, even though they are similar... therefor you can't declare one as having a return type, including void. The compiler already knows what a constructor does so it does not need void added.

That's my understanding on it anyways.

In fact, if you add void to a constructor, it becomes an ordinary method - that just happens to have the same name as the class:
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I completely forgot about that Rob. Funny how many things are allowed in programming languages which are terrible ideas
 
anuj thite
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Brian Legg wrote:All methods must have a return type. I believe that "void" is just to allow the compiler know that a particular method will have a return type of nothing aka will not return any data. A constructor on the other hand is not a method, even though they are similar... therefor you can't declare one as having a return type, including void. The compiler already knows what a constructor does so it does not need void added.

That's my understanding on it anyways.

In fact, if you add void to a constructor, it becomes an ordinary method - that just happens to have the same name as the class:



Thanks a lot Rob.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic