This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes  Error:   'void' type not allowed here Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark " Error:   Watch " Error:   New topic
Author

Error: 'void' type not allowed here

sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

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 ]

Regards, Sud.
SCJP 5 ScjpFAQ JLS
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 02, 2008
Posts: 813

So it is all about printing,right?
Fine.

Thank you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
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

Joined: Apr 02, 2008
Posts: 813

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 02, 2008
Posts: 813

I am sorry I did not get you.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10040
    
    6

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 ]

Never ascribe to malice that which can be adequately explained by stupidity.
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

Thank You


Santhi Bharath
Ranch Hand

Joined: Jun 03, 2008
Posts: 75
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();


thanks and regards<br />Santhi Bharath<br />SCJP 5.0, SCWCD 5.0
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
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

Joined: Jun 03, 2008
Posts: 75
thank you for your reply.i can understand now what i have sent and what you have told
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
You're welcome
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

Thank you all.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
You're welcome
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Error: 'void' type not allowed here
 
Similar Threads
Difference between Dependency and Association
why protected modifier is not allowed?
How to access a class defined within a class in WebService
not getting how exactly spring Depedency Injection works.
Encapsulation, coupling and cohesion