• 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

Example for Certification book doesnt work

 
Greenhorn
Posts: 7
IBM DB2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am newbie to java, started learning after completing 7 years in Cobol development. I am preparing for Certification exam, and need your help in following.

I copied the sample code from SCJP 6 guide to play around with it, and it doesn't run in my machine. Can someone assist. The code is taken from Chapter 2, question 8 from section Q&A:Self Test.

Error that I get in eclipse is

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Dogshow cannot be resolved to a type
at TestQ.DogShow.main(DogShow.java:5)

Thanks !

Code -


 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vaibhav,
Welcome to JavaRanch
You have a class declared as DogShow but in the main method you are referring to it as Dogshow. So the compiler is not able to find class of Dogshow type.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And apart from that, the way the objects are created is- you specify the new operator followed by a call to the constructor which initializes the object. By default if you dont specify any constructor Java provides a default no-arg constructor like:


the above is equivalent to:

because the compiler adds the no-arg (no argument) constructor to the class.

Now you create instance of your class by:


in case of methods which are static, you would not need an instance to invoke them and can use:


otherwise you would require an instance to invoke non-static methods, something like


You would have to edit your code at 2 places where you are instantiating a class.
 
Vaibhav B Sharma
Greenhorn
Posts: 7
IBM DB2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for quick response, and sorry, that was my typo. But it still doesn't work for anything that I declare as an object without assigning to reference type. There are two such errors that I am getting -

C:\JAVA\Packone\Source>javac DogShow.java
DogShow.java:5: error: cannot find symbol
new DogShow.go();
^
symbol: class go
location: class DogShow
DogShow.java:8: error: cannot find symbol
new Hound.bark();
^
symbol: class bark
location: class Hound
2 errors

Code as per below -

 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vaibhav B Sharma wrote:Hi,

Thanks for quick response, and sorry, that was my typo. But it still doesn't work for anything that I declare as an object without assigning to reference type. There are two such errors that I am getting -


I think I pointed out this issue in my second post. As I overlooked the way you were creating the objects in my first post.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the way you are creating the instance would be:
 
Vaibhav B Sharma
Greenhorn
Posts: 7
IBM DB2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Mohamed, didn't see your complete reply before posting my query. I still have a few questions -

1. Since I need an instance to invoke non-static method, I am creating one already -

new Hound.bark();

It works if I create the object, assign it to reference variable, and then use that reference variable to invoke bark() -

Hound h = new Hound();
h.bark();

but why doesn't it work if I just create and provide an instance of Hound on which I want to run bark() on ? I don't want to use reference variable, since I don't care if object is lost after method is complete.


2. This coding technique is used in many examples in SCJP 6 certification guide. So am I missing something here ?

Thanks again for your prompt response.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vaibhav B Sharma wrote:
1. Since I need an instance to invoke non-static method, I am creating one already -

new Hound.bark();

It works if I create the object, assign it to reference variable, and then use that reference variable to invoke bark() -

Hound h = new Hound();
h.bark();


You are missing the (). new Hound().bark(); is the correct syntax for creating the object.
 
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

Vaibhav B Sharma wrote:
It works if I create the object, assign it to reference variable, and then use that reference variable to invoke bark() -

Hound h = new Hound();
h.bark();

but why doesn't it work if I just create and provide an instance of Hound on which I want to run bark() on ?



You mean new Hound().bark()? If the above works, with the "h" variable, then this will also work. If you're saying it doesn't, then you're doing something else different.
 
Vaibhav B Sharma
Greenhorn
Posts: 7
IBM DB2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was a silly, silly error on my part !!

Thanks for your inputs.. Have a nce weeked !
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic