• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stuck with acess modifiers  RSS feed

 
rizwana mujawar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a code



My first class will compile fine but when i try to compile the second class it is throwing error that void type in my class zoo is not allowed... I dint get the logic... Can anyone help to improve my knowledge of access modifier usage..
Thanks
 
dennis deems
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use a method invocation in an expression, you are saying "invoke this method and use the value it returns as an operand in the expression". Here you are trying to perform String concatenation with the value returned by Zoo.run. So tell us, what value is that?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are new, I have edited your post by adding code tags, which you should always use. See how much better it looks now
 
rizwana mujawar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You so much.... I got to add String instead of void because it will be used as operand in other class.... Thanks ya guys. Keep helping me like this to understand java better
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rizwana mujawar wrote:Thank You so much.... I got to add String instead of void because it will be used as operand in other class.... Thanks ya guys. Keep helping me like this to understand java better


Possibly not a good idea. Why would your run() method return a value? What is run()'s job, and what does its return value actually mean?

And defining methods like run() and sleep() that have the same name as common methods in the core API but that have totally different behavior can lead to lots of confusion.

And naming methods with names that don't describe what they actually do can lead to lots of confusion.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i thought all classes inherited a default no parameter constructor from the class Object. if i am wrong, try adding a "do nothing" no parameter constructor to Zoo class
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:i thought all classes inherited a default no parameter constructor from the class Object.


They do, as long as you don't provide any explicit c'tor.

Not that that has anything at all to do with this topic though.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe the OP just worded the exception wrong. sounded to me like he was having a problem with
Zoo z=new Zoo();

i see now Dennis was right. you are trying to use a return value from a method that doesn't return anything.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:i thought all classes inherited a default no parameter constructor from the class Object.
Not really, no. What JV means is that the javac tool adds a default constructor if you don’t write your own constructor. You will find hints and suggestions that default constructors are a bad idea; you notice on that javadoc page they say you should always write a constructor.
if i am wrong, try adding a "do nothing" no parameter constructor to Zoo class
Adding code “because it seems to work” is a good way to create bad code. You write a constructor in order to establish the class invariant. If there is a part of the invariant which a constructor doesn’t establish, that constructor should be changed (or if overloaded, deleted).
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rizwana mujawar wrote:My first class will compile fine but when i try to compile the second class it is throwing error that void type in my class zoo is not allowed... I dint get the logic... Can anyone help to improve my knowledge of access modifier usage.

The problem does not have anything to do with access modifiers (public / protected / private).

The run() method doesn't return any value (it's void), so you can't print its return value, which is what you're trying to do in line 6 of Moo.java.

Just call it like this instead:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!