• 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

Stuck with acess modifiers

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79036
375
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:)
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79036
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic