• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error: 'void' type not allowed here

 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, my code is :




The error which I get upon compiling is:

D:\sudipto nw\Sumo.java:56: 'void' type not allowed here
System.out.println("New Tyre for"+c.getName());
^
1 error


I just want to print the name of the car which invoked the getTyre() method.
The code works fine if the return type of the method getName() is changed to String.
This error is a new one for me.
Is there any other way to do this,in a more simpler way?



[ September 20, 2008: Message edited by: Sudipto Shekhar ]
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can not do this


because method return type is Void ... you trying to print valueless thing right?...thats wat the error is ..

Hope This Helps
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it is all about printing,right?
Fine.

Thank you.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it is not at all about printing.

The error is that a getXXX method usually returns a reference to XXX. So your getName() method ought to have String as its return type and should return the variable in question. Single statement in the method, nothing else. No print statements in the getXXX method (unless you need them for debugging).

Correct that, and your print method will work nicely.
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, according to OO 'get' should return some value!
But here I don't want to return something.

All I want to do is when the getName() is called,
it prints the name on the output stream.

So why do I need a method returning a value?


 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sudipto Shekhar:
All I want to do is when the getName() is called,
it prints the name on the output stream.
So why do I need a method returning a value?



what about this ?


Hope This Helps
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry I did not get you.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you say
System.out.println("New Tyre for"+<something>) ;


you are saying "take this string literal 'New Tyre for', and concatenate it with the string..." and then you NEED A STRING.

you can have a string literal, a reference to a string, or a method that returns a string. Those are your only options.

What you have after the '+' is none of those three things.

your options are to change what is there, change what the method returns, or get rid of the "+ <something>" all together. it sounds like what you want would be along the lines of


NOTE: Having a "getXXX" method that does not GET the value, but instead PRINTS the value is a poor choice. If someone comes along later and sees a method called "getXXX", they will assume it does what the convention says, and returns the value, not print it. I would encourage you to either a) change the method to return a string, or b) change the method name to be "printXXX".
[ September 21, 2008: Message edited by: fred rosenberger ]
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You


 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

as i am a novice,i am not sure about my self.

but, how about this one.

object_name.getClass().getName();

in your case

c.getClass().getName();
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That will return a name, Santhi Bharat, but almost certainly not the name wanted.
[edit]Add: That instruction will get the name of the class that object was created from, not the name field in this class.
The custom is to use (??public) String getName() which returns the name field.[/edit]
[ September 21, 2008: Message edited by: Campbell Ritchie ]
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for your reply.i can understand now what i have sent and what you have told
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic